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!

How to select a record based on the contents of a prior record

Status
Not open for further replies.

GAO

Programmer
Jul 23, 1999
3
US
I have what I hope is an easy question. I am new to SAS, but I have an extensive IT background.

I have a data file that I want to process in SAS. The general problem is that I only want to process a record if the prior record in the file has a particular value (i,e,. the amt field in the prior record must be negative).

I understand how to subset data based on values in the current record, but since SAS processes record-by-record or observation-by-observation, how do I select a record for processing based on the value of a prior record?

Any thoughts or suggestions would be appreciated.

Thanks,

Gary

 
Hi Gary,

the processing very much depends on your file format (I assume you want to read in flat ASCII). To get you started a little framework:

Code:
filename somefile "c:\temp\testme.txt";
data test (drop=dropIt flag);
   length dropIt  $10
          id      $10
		  details $10
		  flag    $1
          ;
   infile someFile missover DLM=" ";
   input id flag;
   if flag eq 'Y'
      then input dropit;
	  else input details;
run;

The file read in contains this:
Code:
MIS N
Management
STRAT N
Strategy
CONTRACTS Y
notRead
FIN Y
notRead
IT N
WORKS!

Result is that the lines 'notRead' are not read :) because of the 'Y' flag in the previous line. I am pretty sure there is a nicer way, but it's a start.

For some reading search for "Formatted Input" in the SAS Help (have this from 8.2).

Cheers,
Matthias
 
Hi again,

in case you want to work with existing tables (but 'previous record' depends on sort order...!), you should have a look at the 'retain' statement in SAS Base. If you get stuck, let me know a few more details on your data structure and requirements.

Cheers,
Matthias
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top