LECTURE 3 FEB 13th , 2007



LECTURE 3 FEB 17th , 2009

 

Data recoding 

Example1: Using IF-THEN/ELSE Statements to Recode a Variable

data recode;

   set grades;

   if       0 LE score LT 65 then grade=0;

   else if 65 LE score LT 70 then grade=1;

   else if 70 LE score LT 80 then grade=2;

   else if 80 LE score LT 90 then grade=3;

   else if       score GE 90 then grade=4;

run;

 

Example2: Using a SELECT Statement to Recode a Variable

data recode;

   set grades;

   select;

       when (0 LE score LT 65) grade=0;

       when(65 LE score LT 70) grade=1;

       when(70 LE score LT 80) grade=2;

       when(80 LE score LT 90) grade=3;

       when(score GE 90)       grade=4;

end;

run;

 

Example3: Using a PUT Function to Create a New Variable---Recoding with the FORMAT statement and the PUT function

Proc format;

   value scorefmt  0-64=’Fail’

                  65-69=’Low Pass’

                  70-79=’Pass’

                  80-89=’High Pass’

                  90-High=’Honors’

;

run;

data new;

   set grades;

   category=put(score, scorefmt.);

run;

 

SET and merge

-------Reading and Combining SAS Data Sets

Example4: Subsetting a SAS Date Set: Selecting Observations That Meet Certain Condidtions

Libname mary ‘C:\employee\jobdata’;

  

Data NJemploy;

    Set mary.employ;

if  state EQ ‘NJ’;

run;

 

data NONJemploy;

   Set mary.employ;

   if state = 'NJ' then delete;

run;

 

Example5: Keep and Drop Statements

Libname mary ‘C:\employee\jobdata’;

  

Data NJemploy;

    Set mary.employ (keep=ID Dept State);

where  state EQ ‘NJ’;

Drop state;

run;

 

Example6: Adding Variables from One Data Set to Another Based on an Identifying Variable

 Proc sort data=demog;

    By id;

Run;

 

Proc sort data=employee;

    By id;

Run;

 

Data combined;

Merge demog employee;

    By id;

    Run;

 

Example7: Controlling Which Observations are Added to the Merged Data Set

Data both;

Merge demog employee (IN=EMP);

    By id;

    If EMP=1;

    Run;

 

Data both;

Merge demog (IN=DEM) employee (IN=EMP);

    By id;

    If DEM=1 and EMP=1;

    Run;

 

Some SAS Functions

-------Data Translation Tools

 

Example8: Mathematical Transformations of Numeric Variables

Data Transfrm;

    Set hospital;

Loglos=log(los);

Xprop=arsin(sqrt(prop));

run;

 

Example9: Character-to-Numeric Conversion, Using the INPUT Function

Variable=INPUT(variable, informat);

 

Data temper;

    input dummy $ @@;

if dummy=’N’ then temp=98.6;

   else temp=input(dummy, 5.);

drop dummy;

datalines;

99.7 N 97.9 N N 112.5

run;

 

Example10:  Numeric-to-Character Conversion, Using the PUT Function

Data one_char;

    Set one (rename=(id=ss));

id=put(ss, ssn11.);

drop ss;

run;

 

Set dataset (Rename=(oldvar1=newvar1 oldvar2=newvar2));

 

Example11:  Taking Substrings

Data set OLD

  ID

  427NJ

  125NY

Data new;

    Set old /* old contains id */;

Length state $ 2 id 3;

State=substr(id,4,2);

id=substr(id,1,3);

run;

     

      substr (char_variable, starting_position, length)

 

Example12:  Reading Combinations of Numeric and Character Data

Data convert;

input dummy $ @@;

dummy2=substr(dummy, 1, length(dummy)-1);

if index (dummy, ‘I’) NE 0 then

    cm=2.54*input (dummy2, 5.);

else if index (dummy, ‘C’) NE 0 then

    cm=input (dummy2, 5.);

    else cm=.;

drop dummy dummy2;

datalines;

23C 100I 12I 133C

35I 45C 35 47I

run;

 

Example13:  Joining (concatenating) Two Strings

Data new;

    Set names;

Name=trim(First) || ‘ ’ || Last;

Keep name;

run;

  

I. working with Dates in the SAS System

                     INformats for Date Values

MMDDYY8.       reads dates written as mm/dd/yy              02/13/92   02-13-92

MMDDYY10.     reads dates written as mm/dd/yyyy    02/13/1992

DATE7.       reads dates in the form ddMMMyy           13Feb92

DATE9.       reads dates in the form ddMMMyyyy        13Feb1992

Example14: Creating a SAS Date from Month, Day, and Year

Data MDYEXAMP;

    Input day month year;

Date=MDY (month, day, year);

Format date worddate.;     /* format date mmddyy10. */

       Datalines;

      12   11   1992

      11   09   1989

run;

 

Example15: Computing Age-----an approximate value for age

Data age;

    Set employee;

Age1=int((‘01Jan95’D-DOB)/365.25);

Age2=round((‘01Jan95’D-DOB)/365.25, 1);

Age3=int((today()-DOB)/365.25);

Age4=round((today()-DOB)/365.25, 1);

run;

 

II. SAS Arrays

Grouping Variables into Arrays

In DATA step programming you can put variables into a temporary group called an array. To define an array, use an ARRAY statement. A simple ARRAY statement has the following form:

|ARRAY array-name{number-of-variables} variable-1 < . . . variable-n>; |

The array-name is a SAS name that you choose to identify the group of variables. The number-of-variables, enclosed in braces, tells SAS how many variables you are grouping, and variable-1< . . . variable-n> lists their names.

Note:    If you have worked with arrays in other programming languages, note that arrays in SAS are different from those in many other languages. In SAS, an array is simply a convenient way of temporarily identifying a group of variables by assigning an alias to them. It is not a permanent data structure; it exists only for the duration of the DATA step. The array-name identifies the array and distinguishes it from any other arrays in the same DATA step; it is not a variable.  

The following ARRAY statement lists the 105 variables:

array XXX[105]  x1-x100 A B C D E;

This statement tells SAS to

·    make a group named XXX for the duration of this DATA step

·    put 105 variable names in XXX: x1-x100, A, B, C, D, and E.

In addition, by listing a variable in an ARRAY statement, you assign the variable an extra name with the form array-name [position], where position is the position of the variable in the list (1 to 105 in this case). The position can be a number, or the name of a variable whose value is the number. This additional name is called an array reference, and the position is called the subscript. The previous ARRAY statement assigns to x1 the array reference XXX[1]. From that point in the DATA step, you can refer to the variable by either its original name or by its array reference. For example, the names A and XXX[101] are equivalent.

 

Example16: Substituting One Value for Another in a Group of Variables

Data new;

    Set old;

array xxx[105] x1-x100 A B C D E;

do i = 1 to 105;

if xxx[i] =999 then xxx[i]=.;

end;

        drop i;

run;

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download