All,
I am new to SAS Programming and am trying to learn as I practice. I have what could be a novice's question - how do I reference a specific observation of a SAS data set ? For e.g.:
Data _null_;
If 0 Then Set Sashelp.class nobs= n;
Call Symputx('TotObs',n);
Stop;
Run;
Data Test;
set Sashelp.class;
Do i = 1 To TotObs;
If i = 1 Then Test = height ;
Else = height[i-1] - 1; /* How to reference the previous observation of the Height column ? */
End;
Run;
Any help is appreciated. Please provide solutions that do not use Proc IML as I don't have it.
You're using IML type terminology, referencing specific cells and rows.
If you want the previous value, the LAG and DIF function are also useful, but they're queue functions so make sure you understand how they work.
RETAIN will hold a value across rows if needed.
You have three major issues with your code that I can see:
A SAS data step loops through the data set automatically so you don't have to loop it yourself. I suspect your loop is entirely unnecessary, as well as the previous step to figure out the number of observations.
Your ELSE statement also is incorrect because it's ELSE= which would create a variable called ELSE that holds some value.
To reference the first row you can use _n_ which is a step counter (not a row counter) so be careful with it. But its good for identifying the first record in a data set.
If you want to understand the basics of a data step or basic SAS processing I highly recommend the Programming 1 e-course which is freely available.
This is probably what you're looking for:
Data Test;
set Sashelp.class;
x=lag(height);
If _n_ = 1 Then Test = height ;
else test = x - 1;
Run;
In the majority of cases (definitely not all cases), you don't measure across observations. Instead, you create a new variable and retain its value moving forward. For example:
data test;
set sashelp.class;
if _n_=1 then test = height;
else test = test - 1;
retain test;
run;
One thing you need to get used to is that SAS doesn't process all the observations at once. It processes just one observation at a time.
Additional related tools: the BY statement in a DATA step (first. and last. variables), and the LAG function.
You're using IML type terminology, referencing specific cells and rows.
If you want the previous value, the LAG and DIF function are also useful, but they're queue functions so make sure you understand how they work.
RETAIN will hold a value across rows if needed.
You have three major issues with your code that I can see:
A SAS data step loops through the data set automatically so you don't have to loop it yourself. I suspect your loop is entirely unnecessary, as well as the previous step to figure out the number of observations.
Your ELSE statement also is incorrect because it's ELSE= which would create a variable called ELSE that holds some value.
To reference the first row you can use _n_ which is a step counter (not a row counter) so be careful with it. But its good for identifying the first record in a data set.
If you want to understand the basics of a data step or basic SAS processing I highly recommend the Programming 1 e-course which is freely available.
This is probably what you're looking for:
Data Test;
set Sashelp.class;
x=lag(height);
If _n_ = 1 Then Test = height ;
else test = x - 1;
Run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.