|
| 1 | +/************************************************************************************************ |
| 2 | + EXAMPLE - SELECT IN DATA STEP |
| 3 | + This program illustrates how to use the SELECT statement in a data step to assign values to a given variable |
| 4 | + based on values of another variable or expression. This is usually more understandable than IF...THEN...ELSE |
| 5 | + constructs, especially when assigning many values. SELECT is similar to a CASE statement in a PROC SQL query. |
| 6 | + Keywords: DATA STEP, CALCULATED COLUMNS |
| 7 | + SAS Versions: SAS 9, SAS Viya |
| 8 | + Documentation: |
| 9 | + - https://documentation.sas.com/doc/en/pgmsascdc/v_062/lestmtsref/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 | +************************************************************************************************/ |
| 21 | + |
| 22 | +data work.cars_orig_country; /*1*/ |
| 23 | + set sashelp.cars(keep=Make Model Origin); /*2*/ |
| 24 | + length Origin_Country $25; /*3*/ |
| 25 | + select (Make); /*4*/ |
| 26 | + when ('Acura') Origin_Country = 'Japan'; /*5*/ |
| 27 | + when ('Honda') Origin_Country = 'Japan'; |
| 28 | + when ('Hyundai') Origin_Country = 'South Korea'; |
| 29 | + when ('Infiniti') Origin_Country = 'Japan'; |
| 30 | + when ('Isuzu') Origin_Country = 'Japan'; |
| 31 | + when ('Kia') Origin_Country = 'South Korea'; |
| 32 | + when ('Lexus') Origin_Country = 'Japan'; |
| 33 | + when ('Mazda') Origin_Country = 'Japan'; |
| 34 | + when ('Mitsubishi') Origin_Country = 'Japan'; |
| 35 | + when ('Nissan') Origin_Country = 'Japan'; |
| 36 | + when ('Scion') Origin_Country = 'Japan'; |
| 37 | + when ('Subaru') Origin_Country = 'Japan'; |
| 38 | + when ('Suzuki') Origin_Country = 'Japan'; |
| 39 | + when ('Toyota') Origin_Country = 'Japan'; |
| 40 | + when ('Audi') Origin_Country = 'Germany'; |
| 41 | + when ('BMW') Origin_Country = 'Germany'; |
| 42 | + when ('Jaguar') Origin_Country = 'England'; |
| 43 | + when ('Land Rover') Origin_Country = 'England'; |
| 44 | + when ('Mercedes-Benz') Origin_Country = 'Germany'; |
| 45 | + when ('MINI') Origin_Country = 'England'; |
| 46 | + when ('Porsche') Origin_Country = 'Germany'; |
| 47 | + when ('Saab') Origin_Country = 'Sweden'; |
| 48 | + when ('Volkswagen') Origin_Country = 'Germany'; |
| 49 | + when ('Volvo') Origin_Country = 'Sweden'; |
| 50 | + when ('Buick') Origin_Country = 'United States of America'; |
| 51 | + when ('Cadillac') Origin_Country = 'United States of America'; |
| 52 | + when ('Chevrolet') Origin_Country = 'United States of America'; |
| 53 | + when ('Chrysler') Origin_Country = 'United States of America'; |
| 54 | + when ('Dodge') Origin_Country = 'United States of America'; |
| 55 | + when ('Ford') Origin_Country = 'United States of America'; |
| 56 | + when ('GMC') Origin_Country = 'United States of America'; |
| 57 | + when ('Hummer') Origin_Country = 'United States of America'; |
| 58 | + when ('Jeep') Origin_Country = 'United States of America'; |
| 59 | + when ('Lincoln') Origin_Country = 'United States of America'; |
| 60 | + when ('Mercury') Origin_Country = 'United States of America'; |
| 61 | + when ('Oldsmobile') Origin_Country = 'United States of America'; |
| 62 | + when ('Pontiac') Origin_Country = 'United States of America'; |
| 63 | + when ('Saturn') Origin_Country = 'United States of America'; |
| 64 | + otherwise Origin_Country = ''; /*6*/ |
| 65 | + end; /*7*/ |
| 66 | + rename Origin = Origin_Region; /*8*/ |
| 67 | +run; |
0 commit comments