Skip to content

Commit ff8f6df

Browse files
2 parents 4cba0e6 + 6062951 commit ff8f6df

13 files changed

+269
-121
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*/
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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;

By Syntax/FORMAT Procedure/Create User-Defined Formats.sas renamed to By Syntax/FORMAT Procedure/1_Create User-Defined Formats.sas

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,20 @@ proc format;
5454
5001-7500 = "Heavyweight Vehicles"
5555
7501-high = "Super Heavy-Duty Vehicles"
5656
;
57-
5857
run;
5958

6059

6160
data cars_formatted; /*2*/
6261
set sashelp.cars;
6362
keep Model MSRP EngineSize MPG_Highway Weight Cylinders;
64-
format Cylinders cyl. EngineSize esize. MPG_Highway mpg. MSRP msrp. Weight weight.;
65-
63+
format Cylinders cyl. EngineSize esize. MPG_Highway mpg. MSRP msrp. Weight weight.;
6664
run;
6765

6866

6967
proc catalog cat=work.formats; /*3*/
7068
contents;
71-
7269
run;
7370

7471

7572
proc format lib=work fmtlib; /*4*/
76-
7773
run;
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/************************************************************************************************
1+
/************************************************************************************************
22
OPTIONS FOR THE VALUE STATEMENT
33
This program creates a format to display data or to use with a put statement or function.
44
Keywords: PROC FORMAT, FORMAT, VALUE
@@ -14,33 +14,34 @@
1414
5. In the PROC MEANS use the mlf option with the order=option to generate statistics using both ranges.
1515
************************************************************************************************/
1616

17-
proc format;/*1*/
17+
proc format; /*1*/
1818
value $gen (default=1) 'F' = 'Female'
19-
'M' = 'Male';/*2*/
19+
'M' = 'Male'; /*2*/
2020
value ht (fuzz=1) low-57 = 'Below Average'
2121
57-67 = 'Average'
22-
67-high = 'Above Average';/*3*/
22+
67-high = 'Above Average'; /*3*/
2323
run;
2424

2525
title "Listing of SASHELP.CLASS";
26-
title2 "With the DEFAULT= Option for sex set to 1";
26+
title2 "DEFAULT=1 Option Set for $GEN. Format Applied to Sex Column";
27+
title3 "FUZZ=1 Option Set for HT. Format Applied to Height Column";
2728
proc print data=sashelp.class noobs;
28-
format sex $gen. height ht.; /*4*/
29+
format sex $gen. height ht.;
2930
run;
3031

31-
proc format;
32-
value height (multilabel) /*4*/
32+
proc format; /*4*/
33+
value height (multilabel)
3334
low-57 = '1 Below Average'
3435
57-<67 = '2 Average'
35-
67-high = '3 Above Average'/*3*/
36+
67-high = '3 Above Average'
3637
low-65 = 'Shorter'
37-
65<-high = 'Taller';/*3*/
38+
65<-high = 'Taller';
3839
run;
3940

4041
title "Listing of SASHELP.CLASS";
41-
title2 "Showing the grouping based on the formatted value of height";
42+
title2 "MULTILABEL Option Groups Values into Multiple Formatted Categories";
4243
proc means data=sashelp.class;
4344
var weight;
4445
class height / mlf order=formatted;
45-
format height height.; /*5*/
46+
format height height.; /*5*/
4647
run;

USING DATE DIRECTIVES ON THE PICTURE STATEMENT.sas renamed to By Syntax/FORMAT Procedure/Using date directives on the PICTURE statement.sas

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
/************************************************************************************************
22
USING DATE DIRECTIVES ON THE PICTURE STATEMENT
33
This program creates formats to display dates in a customized way.
4-
Keywords: PROC FORMAT, FORMAT, PICTURE, DATATYPE=
4+
Keywords: PROC FORMAT, FORMAT, PICTURE, DATATYPE
55
SAS Versions: SAS 9, SAS Viya
66
Documentation: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=default&docsetId=proc&docsetTarget=p1xidhqypi0fnwn1if8opjpqpbmn.htm
7-
1. The picture statement can create formats that can be used to display dates. Start by using PROC FORMAT with a PICTURE
8-
statement that has a datatype= option.
9-
2. The picture statement identifies the name of the new format. It then specifies the range of values to which a template
7+
1. The PICTURE statement can create formats that can be used to display dates. Start by using PROC FORMAT with a PICTURE
8+
statement that has a DATATYPE= option.
9+
2. The PICTURE statement identifies the name of the new format. It then specifies the range of values to which a template
1010
will be applied. Use the LOW and HIGH to keywords to indicate that the range includes everything from the lowest value
1111
to the highest.
12-
3. It then specifies a template or pattern for the format. In this case we use date directives to define the template. In
13-
quotes this template includes three date directives - %0d specifies that the day of the month be written out in one or two
12+
3. The template or pattern for the format is specified. In this case we use date directives to define the template. The quoted
13+
template includes three date directives - %0d specifies that the day of the month be written out in one or two
1414
digits, %b specifies that the month be written out in a three letter abbreviation, and %Y specifies that a 4-digit
15-
year be written out. These elements are separated by the tilde character (~). The pattern closes with spaces to account for
15+
year be written out. These elements are separated by the tilde character (~). The pattern closes with extra spaces to account for
1616
the length of labels.
17-
4. Next, in parentheses, the datatype is specified. The datatype= option can specify that the format is for a date,
18-
a time, or a datetime.
19-
5. In the PROC PRINT apply the format and see the results.
20-
6. Text can be combined with picture templates to create truly customized formats. Text be can be used for some levels of a
17+
4. The DATATYPE= option specifies that the format is a date, a time, or a datetime.
18+
5. Apply the format in PROC PRINT to view the results.
19+
6. Text can be combined with picture templates to create truly customized formats. Text be can be used for some levels of a
2120
format and date directives for other levels.
2221
7. SAS date constants (as well as time constants and datetime constants) can be used to specify the beginning and end of
2322
a date range.
24-
8. In using date directives remember that case can matter. A lower case b results in a 3-letter abbreviation for month, while
25-
an upper case B spells out the whole month.
23+
8. When using date directives, remember that case can matter. A lower case 'b' results in a 3-letter abbreviation for month, while
24+
an upper case 'B' spells out the whole month.
2625
************************************************************************************************/
2726

2827
proc format;

USING DIGIT SELECTORS IN THE PICTURE STATEMENT.sas renamed to By Syntax/FORMAT Procedure/Using digit selectors in the PICTURE statement.sas

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ data delay;
4242
;
4343
run;
4444

45-
proc format;/*1*/
45+
proc format; /*1*/
4646
picture delGrp
47-
low-< 0 = 'Early Arrival' /*2*/
47+
low-< 0 = 'Early Arrival' /*2*/
4848
0 = 'No Delay'
4949
1 = '1 Minute Delay'
50-
1 <- 10 = '09 Minutes Delay' /*3*/
51-
10<-high= ' Delay Greater than 10 Minutes' (noedit);/*4*/
50+
1 <- 10 = '09 Minutes Delay' /*3*/
51+
10<-high= ' Delay Greater than 10 Minutes' (noedit); /*4*/
5252
picture delGx
5353
low-< 0 = 'Early Arrival'
5454
0 = 'No Delay'
@@ -63,7 +63,7 @@ proc format;/*1*/
6363
10<-high= ' Delay Greater than 10 Minutes';
6464
run;
6565

66-
proc report data=delay nowd; /*5*/
66+
proc report data=delay nowd; /*5*/
6767
column delay delay=del delay=dx delay=dely;
6868
define dely / display format=comma12. 'Comma format';
6969
define delay / display format=delgrp. 'NOEDIT with 09';

0 commit comments

Comments
 (0)