/*******************************************************************
This command file for SAS demonstrates how to do some recodes in a
SAS data step, using comparison operators.
********************************************************************/
options linesize=95 pagesize=58;
title;
proc format;
value agefmt 1='Under 6'
2='6 to 9'
3='10 and Older' ;
value $testfmt 'A'='Group A'
'B'='Group B';
data recode;
length school $ 9;
input sex $ 1-3 age 4-6 testgrp $ 8-9 school $ 11-19 ;
if age = 99 then age = . ;
if school = 'NA' then school = ' ' ;
if testgrp = 'NA' then testgrp = ' ' ;
if age ne . then do;
if age lt 6 then agegrp = 1;
if age ge 6 and age lt 10 then agegrp = 2;
if age ge 10 then agegrp = 3;
end;
if agegrp=1 and testgrp='A' then agetest=1;
if agegrp=1 and testgrp='B' then agetest=2;
if agegrp=2 and testgrp='A' then agetest=3;
if agegrp=2 and testgrp='B' then agetest=4;
if agegrp=3 and testgrp='A' then agetest=5;
if agegrp=3 and testgrp='B' then agetest=6;
if school in ('Moore','Bachman') then region = 'East';
else if school = 'White' then region = 'West';
if agegrp in (1,2) then agecat=1;
else if agegrp = 3 then agecat=2;
format agegrp agefmt. testgrp $testfmt. ;
cards;
F 5 B Moore
F 99 B NA
M 7 A Bachman
M 11 B White
M 5 A Bachman
M 10 A Bachman
F 6 B Moore
F 9 NA White
F 8 B Moore
M 4 A Bachman
M 9 B White
F 10 A White
;
proc print data=recode;
title 'PRINTOUT OF DATA WITH FORMATS ASSIGNED TO TESTGRP AND
AGEGRP';
run;
proc print data=recode;
format agegrp testgrp ;
title 'PRINTOUT OF DATA AS IT WAS ORIGINALLY READ INTO
SAS';
run;
proc freq data=recode;
tables school region sex agegrp agecat testgrp agegrp*testgrp
agetest;
title 'FREQUENCY TABLES';
run;
The output from this program is shown below:
PRINTOUT OF DATA WITH FORMATS ASSIGNED TO TESTGRP AND AGEGRP
OBS SCHOOL SEX AGE TESTGRP AGEGRP AGETEST REGION AGECAT
1 Moore F 5 Group B Under 6 2 East 1
2 F . Group B . . .
3 Bachman M 7 Group A 6 to 9 3 East 1
4 White M 11 Group B 10 and Older 6 West 2
5 Bachman M 5 Group A Under 6 1 East 1
6 Bachman M 10 Group A 10 and Older 5 East 2
7 Moore F 6 Group B 6 to 9 4 East 1
8 White F 9 6 to 9 . West 1
9 Moore F 8 Group B 6 to 9 4 East 1
10 Bachman M 4 Group A Under 6 1 East 1
11 White M 9 Group B 6 to 9 4 West 1
12 White F 10 Group A 10 and Older 5 West 2
PRINTOUT OF DATA AS IT WAS ORIGINALLY READ INTO SAS
OBS SCHOOL SEX AGE TESTGRP AGEGRP AGETEST REGION AGECAT
1 Moore F 5 B 1 2 East 1
2 F . B . . .
3 Bachman M 7 A 2 3 East 1
4 White M 11 B 3 6 West 2
5 Bachman M 5 A 1 1 East 1
6 Bachman M 10 A 3 5 East 2
7 Moore F 6 B 2 4 East 1
8 White F 9 2 . West 1
9 Moore F 8 B 2 4 East 1
10 Bachman M 4 A 1 1 East 1
11 White M 9 B 2 4 West 1
12 White F 10 A 3 5 West 2
FREQUENCY TABLES
Cumulative Cumulative
SCHOOL Frequency Percent Frequency Percent
-----------------------------------------------------
Bachman 4 36.4 4 36.4
Moore 3 27.3 7 63.6
White 4 36.4 11 100.0
Frequency Missing = 1
Cumulative Cumulative
REGION Frequency Percent Frequency Percent
----------------------------------------------------
East 7 63.6 7 63.6
West 4 36.4 11 100.0
Frequency Missing = 1
Cumulative Cumulative
SEX Frequency Percent Frequency Percent
-------------------------------------------------
F 6 50.0 6 50.0
M 6 50.0 12 100.0
Cumulative Cumulative
AGEGRP Frequency Percent Frequency Percent
----------------------------------------------------------
Under 6 3 27.3 3 27.3
6 to 9 5 45.5 8 72.7
10 and Older 3 27.3 11 100.0
Frequency Missing = 1
Cumulative Cumulative
AGECAT Frequency Percent Frequency Percent
----------------------------------------------------
1 8 72.7 8 72.7
2 3 27.3 11 100.0
Frequency Missing = 1
FREQUENCY TABLES
Cumulative Cumulative
TESTGRP Frequency Percent Frequency Percent
-----------------------------------------------------
Group A 5 45.5 5 45.5
Group B 6 54.5 11 100.0
Frequency Missing = 1
TABLE OF AGEGRP BY TESTGRP
AGEGRP TESTGRP
Frequency |
Percent |
Row Pct |
Col Pct |Group A |Group B | Total
-------------+--------+--------+
Under 6 | 2 | 1 | 3
| 20.00 | 10.00 | 30.00
| 66.67 | 33.33 |
| 40.00 | 20.00 |
-------------+--------+--------+
6 to 9 | 1 | 3 | 4
| 10.00 | 30.00 | 40.00
| 25.00 | 75.00 |
| 20.00 | 60.00 |
-------------+--------+--------+
10 and Older | 2 | 1 | 3
| 20.00 | 10.00 | 30.00
| 66.67 | 33.33 |
| 40.00 | 20.00 |
-------------+--------+--------+
Total 5 5 10
50.00 50.00 100.00
Frequency Missing = 2
FREQUENCY TABLES
13
21:29 Wednesday, May 17,
1995
Cumulative Cumulative
AGETEST Frequency Percent Frequency Percent
-----------------------------------------------------
1 2 20.0 2 20.0
2 1 10.0 3 30.0
3 1 10.0 4 40.0
4 3 30.0 7 70.0
5 2 20.0 9 90.0
6 1 10.0 10 100.0
Frequency Missing = 2