|
|
|
||||
|
Help the Stat Consulting Group by
giving a gift
| |||||
|
Loading
|
|||||
| proc format | Creates formats (aka value labels) |
| label | Creates labels for variables |
| if then | Executes a statement only if the condition is true |
Let's see what the data set looks like.
proc contents data = "c:\sas\hs0"; run;
Let's format one of the variables, schtyp. To add value labels to a variable, we need to use proc format. Note that the format is not permanently assigned to the variable. In other words, we need to tell SAS to use the format whenever we want to see the value labels instead of the numbers.
proc format;
value scl 1 = "public"
2 = "private";
run;
proc freq data = "c:\sas\hs0"; tables schtyp; format schtyp scl.; run;
In this data step, we will label the data set and add a variable label to the variable schtyp.
data hs1(label="High School and Beyond, 200 cases"); set "c:\sas\hs0"; label schtyp = "The type of school the student attended."; run;
We will now look at the effects of the data step using proc contents.
proc contents data = hs1; run;
Now we will run a longer data step to do a variety of tasks. Comments are used to explain the program.
proc format;
* create value labels for schtyp ;
value scl 1 = "public"
2 = "private";
* create value labels for grade ;
value abcdf 0 = "F"
1 = "D"
2 = "C"
3 = "B"
4 = "A";
* create value labels for female ;
value fm 1 = "female"
0 = "male";
run;
* create data file hs1 and label it ;
data hs1(label="High School and Beyond, 200 cases");
set "c:\sas\hs0"; /* this reads in the sas file c:\sas\hs0 */
* the label statement labels the variable schtyp ;
label schtyp = "The type of school the student attended.";
* the if-then statements create a new variable, called prog,
which is numeric variable ;
if prgtype = "academic" then prog = 1;
if prgtype = "general" then prog = 2;
if prgtype = "vocational" then prog = 3;
* the label statement labels the variable prog ;
label prog = "The type of program in which the student was enrolled.";
* create a new variable, called female,
which is identical to the variable gender ;
* and then use drop to drop gender ;
female = gender;
drop gender;
* the label statement labels the variable female ;
label female = "The gender of the student.";
* the if statement recodes values of 5 in variable race to be missing (.) ;
if race = 5 then race = .;
* create a variable total that is the sum of read + write + socst ;
total = read + write + socst;
* create a new variable, called grade, which is identical to total ;
grade = total;
* the if-then statements recode grade ;
if (total < 80) then grade = 0;
if (80 <= total < 110) then grade = 1;
if (110 <= total < 140) then grade = 2;
if (140 <= total < 170) then grade = 3;
if (total >= 170) then grade = 4;
* Below we label the variable grade ;
label grade = "These are the combined grades of read, write and socst.";
run;
Let's check to see that everything worked as planned.
proc contents data = hs1; run; proc print data = hs1 (obs = 20); format schtyp scl. female fm. grade abcdf.; run; proc freq data = hs1; format schtyp scl. female fm.; tables schtyp female; run;
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