Reading a Single SAS Data Set to Create Another

Example: Reading a SAS Data Set

The data set Cert.Admit contains health information about patients in a clinic, their activity level, height, and weight. Suppose you want to create a subset of the data. Specifically, you want to create a small data set containing data about all the men in the group who are older than 50.
To create the data set, you must first reference the library in which Cert.Admit is stored. Then you must specify the name of the library in which you want to store the Males data set. Finally, you add statements to the DATA step to read your data and create a new data set.
The DATA step below reads all observations and variables from the existing data set Cert.Admit into the new data set Males. The DATA statement creates the permanent SAS data set Males, which is stored in the SAS library Men50. The SET statement reads the permanent SAS data set Cert.Admit and subsets the data using a WHERE statement. The new data set, Males, contains all males in Cert.Admit who are older than 50.
libname cert 'C:UsersStudent1cert';
libname Men50 'C:UsersStudent1certMen50';
data Men50.males;
  set cert.admit;
  where sex='M' and age>50;   
run;
When you submit this DATA step, the following messages appear in the log, confirming that the new data set was created:
Log 4.3 SAS Log Output
69205  data Men50.males;
69206    set cert.admit;
69207    where sex='M' and age>50;
69208  run;

NOTE: There were 3 observations read from the data set
      CERT.ADMIT.
      WHERE (sex='M') and (age>50);
NOTE: The data set MEN50.MALES has 3 observations and 9
      variables.

You can add a PROC PRINT statement to this same example to see the output of Men50.Males.
proc print data=Men50.males;
   title 'Men Over 50';
run;
Output 4.8 PROC PRINT Output for the Data Set Males
The PROC PRINT output displays three observations and nine variables from data set MEN50.males.

Specifying DROP= and KEEP= Data Set Options

You can specify the DROP= and KEEP= data set options anywhere you name a SAS data set. You can specify DROP= and KEEP= in either the DATA statement or the SET statement. It depends on whether you want to drop variables from either the output data set or the source data set:
  • If you never reference certain variables and you do not want them to appear in the new data set, use a DROP= option in the SET statement.
    In the DATA step shown below, the DROP= or KEEP= option in the SET statement prevents the variables Triglyc and Uric from being read. These variables do not appear in the Cert.Drug1h data set and are not available to be used by the DATA step.
  • If you do need to reference a variable in the original data set (in a subsetting IF statement, for example), you can specify the variable in the DROP= or KEEP= option in the DATA statement. Otherwise, the statement that references the variable uses a missing value for that variable.
    This DATA step uses the variable Placebo to select observations. To drop Placebo from the new data set, the DROP= option must appear in the DATA statement.
    When used in the DATA statement, the DROP= option simply drops the variables from the new data set. However, they are still read from the original data set and are available within the DATA step.
data cert.drug1h(drop=placebo);
  set cert.cltrials(drop=triglyc uric);
  if placebo='YES';
run;
proc print data=cert.drug1h;
run;
Output 4.9 PROC PRINT Output of Cert.Drug1h
PROC PRINT Output of Cert.Drug1h
Last updated: August 23, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.124.21