Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare observations from the same variable in SAS

Status
Not open for further replies.

bumed

Technical User
Aug 23, 2007
18
US
I have a data set in SAS and would like to compare variables from the same column.
Obs Subject Type Code Time
1 1001.01 Picture al 1409567
2 1001.01 Picture altl 1412637
3 1001.01 Response 2 1418426
4 1001.01 Picture ar 1429591
5 1001.01 Picture artr 1432527
6 1001.01 Response 1 1437545
For example if code = “al” followed by “altl” on the next line put a 1 in a new variable Valid. Sorry I’m just getting started with SAS.

 
The RETAIN statement is what you need here.
You should check it out on the online doco
However, the basic code you'll need is this:-
Code:
data dset2;
  set dset1;

  * Set the length and type of lastcode, should be *;
  * same as code.                                  *;
  length lastcode $5;
  * Retain lastcode, initially it'll be empty.     *;
  retain lastcode;

  * Check last value and current value *;
  if lastcode = 'al' and code = 'atl' then valid=1;
  
  * Set lastcode ready for next record *;
  lastcode = code;
run;
Now, you can only check the current record against previous records, so if you're wanting to update the current record depending on the NEXT record, you need to reverse the order of the records by sorting them accordingly (you may need to use the "DESCENDING" keyword in the proc sort), and then use the above (but switch the checks around..)
Enjoy.


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Thanx for your help this worked great. I didn't know about the last* statement
Steve
 
In my example above, LASTCODE is a variable I've created, it isn't some system variable, and could have been called anything. You did remind me of something there though,
you should also look up "by group processing" in the doco, which gives you access to the LAST.xxx and FIRST.xxx objects which can be very very useful.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top