|
| 1 | +/************************************************************************************************ |
| 2 | + CREATE NEW COLUMN WITH THE SELECT STATEMENT |
| 3 | + This program illustrates how to use the SELECT statement in a DATA step to assign values to a |
| 4 | + given variable based on values of another variable or expression. This is an alternative to |
| 5 | + IF/THEN/ELSE syntax and may be preferred, especially when assigning many values. SELECT is |
| 6 | + similar to a CASE expression in a PROC SQL query. |
| 7 | + Keywords: DATA STEP, SELECT, calculated columns |
| 8 | + SAS Versions: SAS 9, SAS Viya |
| 9 | + Documentation: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=default&docsetId=lestmtsref&docsetTarget=p09213s9jc2t99n1vx0omk2rh9ps.htm |
| 10 | + 1. Create a new dataset named CARS_ORIG_COUNTRY in the WORK library. |
| 11 | + 2. Read the existing SASHELP.CARS dataset, but KEEP only the MAKE, MODEL, and ORIGIN variables. |
| 12 | + 3. Create a variable named ORIGIN_COUNTRY as a character variable with a length of 25. |
| 13 | + 4. SELECT the value of the MAKE variable as each observation is processed in the PROGRAM DATA VECTOR (PDV). |
| 14 | + 5. Assign a value to the ORIGIN_COUNTRY variable based on the current value of MAKE, Japan and Acura in this case. |
| 15 | + Values are contained in quotation marks (single quotes in this example) since both variables are of type character. |
| 16 | + 6. Use OTHERWISE to assign a missing value to ORIGIN_COUNTRY for cases when the value of MAKE is not specified in |
| 17 | + the SELECT statement. |
| 18 | + 7. END specifies the end of the SELECT values. |
| 19 | + 8. RENAME the ORIGIN variable to ORIGIN_REGION for clarity between the 2 ORIGIN variables in the resulting dataset. |
| 20 | + 9. An alternate approach allows multiple values listed inside the parentheses, separated by commas. |
| 21 | +************************************************************************************************/ |
| 22 | + |
| 23 | +data work.cars_orig_country; /*1*/ |
| 24 | + set sashelp.cars(keep=Make Model Origin); /*2*/ |
| 25 | + length Origin_Country $25; /*3*/ |
| 26 | + select (Make); /*4*/ |
| 27 | + when ('Acura') Origin_Country = 'Japan'; /*5*/ |
| 28 | + when ('Honda') Origin_Country = 'Japan'; |
| 29 | + when ('Hyundai') Origin_Country = 'South Korea'; |
| 30 | + when ('Infiniti') Origin_Country = 'Japan'; |
| 31 | + when ('Isuzu') Origin_Country = 'Japan'; |
| 32 | + when ('Kia') Origin_Country = 'South Korea'; |
| 33 | + when ('Lexus') Origin_Country = 'Japan'; |
| 34 | + when ('Mazda') Origin_Country = 'Japan'; |
| 35 | + when ('Mitsubishi') Origin_Country = 'Japan'; |
| 36 | + when ('Nissan') Origin_Country = 'Japan'; |
| 37 | + when ('Scion') Origin_Country = 'Japan'; |
| 38 | + when ('Subaru') Origin_Country = 'Japan'; |
| 39 | + when ('Suzuki') Origin_Country = 'Japan'; |
| 40 | + when ('Toyota') Origin_Country = 'Japan'; |
| 41 | + when ('Audi') Origin_Country = 'Germany'; |
| 42 | + when ('BMW') Origin_Country = 'Germany'; |
| 43 | + when ('Jaguar') Origin_Country = 'England'; |
| 44 | + when ('Land Rover') Origin_Country = 'England'; |
| 45 | + when ('Mercedes-Benz') Origin_Country = 'Germany'; |
| 46 | + when ('MINI') Origin_Country = 'England'; |
| 47 | + when ('Porsche') Origin_Country = 'Germany'; |
| 48 | + when ('Saab') Origin_Country = 'Sweden'; |
| 49 | + when ('Volkswagen') Origin_Country = 'Germany'; |
| 50 | + when ('Volvo') Origin_Country = 'Sweden'; |
| 51 | + when ('Buick') Origin_Country = 'United States of America'; |
| 52 | + when ('Cadillac') Origin_Country = 'United States of America'; |
| 53 | + when ('Chevrolet') Origin_Country = 'United States of America'; |
| 54 | + when ('Chrysler') Origin_Country = 'United States of America'; |
| 55 | + when ('Dodge') Origin_Country = 'United States of America'; |
| 56 | + when ('Ford') Origin_Country = 'United States of America'; |
| 57 | + when ('GMC') Origin_Country = 'United States of America'; |
| 58 | + when ('Hummer') Origin_Country = 'United States of America'; |
| 59 | + when ('Jeep') Origin_Country = 'United States of America'; |
| 60 | + when ('Lincoln') Origin_Country = 'United States of America'; |
| 61 | + when ('Mercury') Origin_Country = 'United States of America'; |
| 62 | + when ('Oldsmobile') Origin_Country = 'United States of America'; |
| 63 | + when ('Pontiac') Origin_Country = 'United States of America'; |
| 64 | + when ('Saturn') Origin_Country = 'United States of America'; |
| 65 | + otherwise Origin_Country = ''; /*6*/ |
| 66 | + end; /*7*/ |
| 67 | + rename Origin = Origin_Region; /*8*/ |
| 68 | +run; |
| 69 | + |
| 70 | + |
| 71 | +data work.cars_orig_country; /*9*/ |
| 72 | + set sashelp.cars(keep=Make Model Origin); |
| 73 | + length Origin_Country $25; |
| 74 | + select (Make); |
| 75 | + when ('Acura', 'Honda', 'Infiniti', 'Isuzu', 'Lexus', 'Mazda', 'Mitsubishi', 'Nissan', 'Scion', 'Subaru', 'Suzuki', 'Toyota') |
| 76 | + Origin_Country = 'Japan'; |
| 77 | + when ('Hyundai', 'Kia') |
| 78 | + Origin_Country = 'South Korea'; |
| 79 | + when ('Audi', 'BMW', 'Mercedes-Benz', 'Volkswagen', 'Porsche') |
| 80 | + Origin_Country = 'Germany'; |
| 81 | + when ('Jaguar', 'Land Rover', 'MINI') |
| 82 | + Origin_Country = 'England'; |
| 83 | + when ('Saab', 'Volvo') |
| 84 | + Origin_Country = 'Sweden'; |
| 85 | + when ('Buick', 'Cadillac', 'Chevrolet', 'Chrysler', 'Dodge', 'Ford', 'GMC', 'Hummer', 'Jeep', 'Lincoln', 'Mercury', 'Oldsmobile', 'Pontiac', 'Saturn') |
| 86 | + Origin_Country = 'United States of America'; |
| 87 | + otherwise Origin_Country = ''; |
| 88 | + end; |
| 89 | + rename Origin = Origin_Region; |
| 90 | +run; |
0 commit comments