Reference an observation in sas dataset

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Reference an observation in sas dataset

[ Edited ]

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.


Accepted Solutions
Solution
‎06-11-2018 06:12 PM
Super User
Posts: 23,695

Re: Reference an observation in sas dataset

Posted in reply to UdayGuntupalli

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; 

 

View solution in original post


All Replies
Super User
Posts: 6,759

Re: Reference an observation in sas dataset

[ Edited ]
Posted in reply to UdayGuntupalli

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. 

Solution
‎06-11-2018 06:12 PM
Super User
Posts: 23,695

Re: Reference an observation in sas dataset

Posted in reply to UdayGuntupalli

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; 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 120 views
  • 2 likes
  • 3 in conversation