Posts

Showing posts with the label Do loops

SAS Arrays and DO-LOOPS

SAS Do Array Three SAS Programs that use Arrays

Change all missing values of all variables into zeros/putting zeros in place of missing values for variables

Have you been asked how to convert missing values for all the variables into zeros..... if you are.... here is the answer for that..... In this example the I have used array to do the same. The variable list includes ID and Score1 to score6.Using simple array method we can change all the missing value for the variables score1 to score6 to 0. data old; input ID SCORE1 SCORE2 SCORE3 SCORE4 SCORE5 SCORE6; cards; 24 100 97 . 100 85 85 28 . 87 98 100 . 90 60 100 . . 100 100 100 65 100 98 100 . 90 100 70 99 97 100 100 95 100 40 97 99 98 . 100 95 190 100 . 97 100 100 90 196 100 100 . 100 100 100 210 . 85 . 90 80 95 ;   run ; *Ist Method; data new; set old; array zero score1-score6; do over zero; if zero=. then zero=0 ; end ; run ; *2nd Method; data new; set old; array nums _numeric_; do over nums ; if nums=. then nums=0 ; end; run ; proc print; Title 'Missing values changed to zero using arrays and a do loop' ; run ; Output: I...

LAG Function: How to obtain information from previous observation(s)

Often times SAS® programmers need to retain the value of a variable in the current observation to the next observation. The LAG function  can be very helpful here. A LAGn (n=1-100) function returns the value of the nth previous execution of the function. It is easy to assume that the LAGn functions return values of the nth previous observation. Using the LAG function to obtain information from previous observation(s) **********************************************************; /* Sample 1: Create a single lag of one variable */ data one; input x; lagonce= lag (x); datalines ; 1 2 3 4 5 ; proc print data=one; title 'Sample1: Single lag of one variable' ; run ; ***************************************************************; /* Sample 2: Create multiple lags of one variable */ data two; input x; lag1 = lag (x); lag2= lag2 (x); datalines ; 1 2 3 4 5 ; proc print data=two; title 'Sample 2: Multiple lags of one variable' ; run ; ...