|
1 | 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 |
| 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 |
7 | 8 | SAS Versions: SAS 9, SAS Viya
|
8 |
| - Documentation: |
9 |
| - - https://documentation.sas.com/doc/en/pgmsascdc/v_062/lestmtsref/p09213s9jc2t99n1vx0omk2rh9ps.htm |
| 9 | + Documentation: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=default&docsetId=lestmtsref&docsetTarget=p09213s9jc2t99n1vx0omk2rh9ps.htm |
10 | 10 | 1. Create a new dataset named CARS_ORIG_COUNTRY in the WORK library.
|
11 | 11 | 2. Read the existing SASHELP.CARS dataset, but KEEP only the MAKE, MODEL, and ORIGIN variables.
|
12 | 12 | 3. Create a variable named ORIGIN_COUNTRY as a character variable with a length of 25.
|
13 | 13 | 4. SELECT the value of the MAKE variable as each observation is processed in the PROGRAM DATA VECTOR (PDV).
|
14 | 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. |
| 15 | + Values are contained in quotation marks (single quotes in this example) since both variables are of type character. |
16 | 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. |
| 17 | + the SELECT statement. |
18 | 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. |
| 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. |
20 | 21 | ************************************************************************************************/
|
21 | 22 |
|
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*/ |
| 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*/ |
27 | 28 | when ('Honda') Origin_Country = 'Japan';
|
28 | 29 | when ('Hyundai') Origin_Country = 'South Korea';
|
29 | 30 | when ('Infiniti') Origin_Country = 'Japan';
|
@@ -61,7 +62,29 @@ data work.cars_orig_country; /*1*/
|
61 | 62 | when ('Oldsmobile') Origin_Country = 'United States of America';
|
62 | 63 | when ('Pontiac') Origin_Country = 'United States of America';
|
63 | 64 | when ('Saturn') Origin_Country = 'United States of America';
|
64 |
| - otherwise Origin_Country = ''; /*6*/ |
65 |
| - end; /*7*/ |
66 |
| - rename Origin = Origin_Region; /*8*/ |
| 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; |
67 | 90 | run;
|
0 commit comments