/* The first few examples using SET have been seen before, in the SAS_if_then.sas program. The main data set contains physical fitness measurements and the variables are Age (years), Weight (kg), Oxygen intake rate (ml per kg body weight per minute), time to run 1.5 miles (minutes), heart rate while resting, heart rate while running (same time Oxygen rate measured), and maximum heart rate recorded while running. */ options ls=100; filename foobar url "http://www.uvm.edu/~abh/stat295/datasets/fitness2.dat"; data fitness; infile foobar; input id Sex $1. Age Weight Oxygen RunTime RestPulse RunPulse MaxPulse; run; proc print; title "Fitness Data"; run; data males females; set fitness; if sex = "M" then output males; else if sex = "F" then output females; run; /* The above uses the SET statement and a subsetting IF statement. Same as in the SAS_if_then.sas program. */ /* We can now take the individual male and female data sets and concatenate them with a SET statement to recreate the original fitness data sets. Observations for males are followed by observations for females. */ data fitness; set males females; run; proc print; title "Fitness data recreated from male and female data sets"; title2 "Data sets concatenated"; run; /* The following example sorts each of the individual male and female data sets by age, outputs them to new data sets and drops the sex variable from each one. */ proc sort data=males out=males2(drop=sex); by age; run; proc print data=males2; title "Males (sex variable dropped)"; run; proc sort data=females out=females2(drop=sex); by age; run; proc print data=females2; title "Females (sex variable dropped)"; run; /* The following code recreates the original fitness data set by interleaving the males and females data sets by age. Anytime a BY statement is used in a data set, the data must already be sorted as specified in the BY. The resulting data set is ordered by increasing age. This is also an example of the IN= option of the SET statement. It allows you to test whether an observation is coming from a specific data set. Here we want to put a sex variable back into the data, so we test whether an observation is coming in from the males data. */ data fit_age; set males2(in=m) females2; by age; if m then sex="M"; else sex="F"; run; proc print; title "Fitness data recreated from males and females data sets"; title2 "Interleaved by age - sex variable restored"; run; /* The following code recreates the original fitness data set by interleaving the males and females data sets by sex and age. It is necessary to sort each data set before interleaving them. Both data sets must already be sorted as specified in the BY. The resulting data set is ordered by increasing age within each sex. */ proc sort data=males; by sex age; run; proc sort data=females; by sex age; run; data fit_age; set males females; by sex age; run; proc print; title "Fitness data recreated from males and females data sets"; title2 "Interleaved by sex and age"; run;