Skip to content

Commit b29315a

Browse files
authored
Rename files and update DATA step program
1 parent 63643b9 commit b29315a

9 files changed

+65
-40
lines changed

By Syntax/DATA Step/Compute new columns with expressions and functions.sas

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
/************************************************************************************************/
2-
/* COMPUTE NEW COLUMNS WITH EXPRESSIONS AND FUNCTIONS */
3-
/* This program uses the DATA step to create a new table with selected rows and columns, */
4-
/* new computed columns, and formatted values. */
5-
/* Keywords: DATA STEP, CALCULATED COLUMNS */
6-
/* SAS Versions: SAS 9, SAS Viya */
7-
/* Documentation: https://go.documentation.sas.com/doc/en/pgmsascdc/default/lestmtsref/p1hglxgj1sjhdzn18soqrqmvogvj.htm */
8-
/* 1. Create a new table in the default WORK library named CARS_NEW. */
9-
/* 2. Read the existing table SASHELP.CARS. */
10-
/* 3. Only read rows from the input table where Origin equals Europe. Character values must */
11-
/* be in quotation marks. */
12-
/* 4. Create new columns KmH_Highway and KmH_City that multiply MPG_Highway and MPG_City */
13-
/* by 1.609344. */
14-
/* 5. Create a new column KmH_Average that calculates the mean of KmH_Highway and KmH_City. */
15-
/* 6. Apply the 4.1 format to all variables that begin with KmH. This numeric format */
16-
/* allocates 4 total positions to display the value and rounds to one decimal place. */
17-
/* 7. Keep only the variables in the list in the output table. The colon is used to select */
18-
/* all columns that begin with KmH. */
19-
/************************************************************************************************/
1+
/************************************************************************************************
2+
COMPUTE NEW COLUMNS WITH EXPRESSIONS AND FUNCTIONS
3+
This program uses the DATA step to create a new table with selected rows and columns,
4+
new computed columns, and formatted values.
5+
Keywords: DATA STEP, calculated columns
6+
SAS Versions: SAS 9, SAS Viya
7+
Documentation: https://go.documentation.sas.com/doc/en/pgmsascdc/default/lestmtsref/p1hglxgj1sjhdzn18soqrqmvogvj.htm
8+
1. Create a new table in the default WORK library named CARS_NEW.
9+
2. Read the existing table SASHELP.CARS.
10+
3. Only read rows from the input table where Origin equals Europe. Character values must
11+
be in quotation marks.
12+
4. Create new columns KmH_Highway and KmH_City that multiply MPG_Highway and MPG_City
13+
by 1.609344.
14+
5. Create a new column KmH_Average that calculates the mean of KmH_Highway and KmH_City.
15+
6. Apply the 4.1 format to all variables that begin with KmH. This numeric format
16+
allocates 4 total positions to display the value and rounds to one decimal place.
17+
7. Keep only the variables in the list in the output table. The colon is used to select
18+
all columns that begin with KmH.
19+
************************************************************************************************/
2020

2121
data cars_new; /*1*/
2222
set sashelp.cars; /*2*/

By Syntax/DATA Step/Data_Step_Select.sas renamed to By Syntax/DATA Step/Create new column with the SELECT statement.sas

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
/************************************************************************************************
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
78
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
1010
1. Create a new dataset named CARS_ORIG_COUNTRY in the WORK library.
1111
2. Read the existing SASHELP.CARS dataset, but KEEP only the MAKE, MODEL, and ORIGIN variables.
1212
3. Create a variable named ORIGIN_COUNTRY as a character variable with a length of 25.
1313
4. SELECT the value of the MAKE variable as each observation is processed in the PROGRAM DATA VECTOR (PDV).
1414
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.
1616
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.
1818
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.
2021
************************************************************************************************/
2122

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*/
2728
when ('Honda') Origin_Country = 'Japan';
2829
when ('Hyundai') Origin_Country = 'South Korea';
2930
when ('Infiniti') Origin_Country = 'Japan';
@@ -61,7 +62,29 @@ data work.cars_orig_country; /*1*/
6162
when ('Oldsmobile') Origin_Country = 'United States of America';
6263
when ('Pontiac') Origin_Country = 'United States of America';
6364
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;
6790
run;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

KEYWORDS.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@ PROC PRINT
2020
PROC SORT
2121
PROC TRANSPOSE
2222
PROC UNIVARIATE
23+
SELECT
2324
VALUE
2425

2526

2627
Topic Keywords (lower case)
2728
aggregate
2829
csv
2930
custom format
31+
calculated columns
32+
debug
3033
delimited data
3134
Excel
3235
frequency
3336
graph
3437
import data
3538
plot
3639
read data
37-
report
38-
debug
39-
remove duplicates
40+
remove duplicates
41+
report

0 commit comments

Comments
 (0)