SAS Class Notes 2.0
Managing Data
1.0 SAS statements and procs in this unit
| keep | Keeps just named variables |
| drop | Drops named variables |
| set | Reads in named file(s). If more than one is
named, files are combined (stacked) |
| merge | Merges files |
2.0 Demonstration and Explanation
Example 2.1 - Honor's Thesis
Suppose we are undergraduates working on
our honors thesis and we wish to analyze just a subset of the hs1 data
file. In fact, we are studying "good readers" and just want to
focus on the students who had a reading score of 60 and higher. The
following shows how we can take the hs1 data file and make a separate
folder called honors and store a copy of our data which just has the
students with reading scores of 60 or higher.
data "c:\sas\honors\goodread";
set "c:\sas\hs1";
where (read >=60);
run;
proc means data="c:\sas\honors\goodread";
var read;
run;
Example 2.1, continued - keeping variables
Further suppose that our
data file had many many variables, say 2000 variables, but we only care about just
a handful of them, id female read and write. We can subset
our data file to keep just those variables as shown below.
data "c:\sas\honors\hskept";
set "c:\sas\honors\goodread";
keep id female read write;
run;
proc contents data="c:\sas\honors\hskept";
run;
Example 2.1, continued - dropping variables
Instead of wanting to keep
just a handful of variables, it is possible that we might want to get rid of
just a handful of variables in our data file. Below we show how we could
get rid of the variables ses and prog.
data "c:\sas\honors\hsdropped";
set "c:\sas\honors\goodread";
drop ses prog;
run;
proc contents data="c:\sas\honors\hsdropped";
run;
Example 2.2 - Master's Thesis
Now we have moved on to our master's
thesis. We have a folder called masters and we have been given a
file with the data for the males (called hsmale) and a file for the
females (called hsfemale). We need to combine these files together
to be able to analyze them, as shown below.
proc freq data="c:\sas\masters\hsmale";
tables female;
run;
proc freq data="c:\sas\masters\hsfemale";
tables female;
run;
data "c:\sas\masters\hsmasters";
set "c:\sas\masters\hsmale" "c:\sas\masters\hsfemale";
run;
proc freq data="c:\sas\masters\hsmasters";
tables female;
run;
Example 2.3 - Dissertation
Now we are working on our dissertation
and, as with our masters, we have been given two files. In this case, we
have a file that has all of the demographic information (called hsdem)
and a file with the test scores (called hstest), and we wish to merge
these files together. We show how to do this below.
proc print data="c:\sas\diss\hsdem"(obs=10);
run;
proc print data="c:\sas\diss\hstest"(obs=10);
run;
proc sort data="c:\sas\diss\hsdem" out=dem;
by id;
run;
proc sort data="c:\sas\diss\hstest" out=test;
by id;
run;
data "c:\sas\diss\hsdiss";
merge dem(in=one) test(in=two);
fromdem = one;
fromtest = two;
by id;
run;
proc print data="c:\sas\diss\hsdiss";
run;
proc print data="c:\sas\diss\hsdiss";
var id fromdem fromtest;
run;
proc freq data="c:\sas\diss\hsdiss";
tables fromdem*fromtest ;
run;
3.0 For More Information
UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services
The content of this web site should not be
construed as an endorsement of any particular web site, book, or software
product by the University of California