BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

2 REPLIES 2
Astounding
PROC Star

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. 

Reeza
Super User

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; 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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