Help using Base SAS procedures

Read previous record after a condition

Reply
Frequent Contributor
Posts: 78

Read previous record after a condition

I have data in the raw file as below

********************  TOD=180316  DATE=00115127  NAME=RECYCLE DATA SET ENDED  

     FUNC=RECYCLE MIG VOLUME      TOVOL=E36229  FRVOL=E32423  DEVT=78048081  ST

Now what I am doing with the below code is find FUNC=RECYLCE and then create the variable however while creating the variable I also need the DATE which means I need to go to previous record in the same DATA Step...I see certain ways but haven't come to the conclusion could somebody point please...

DATA OUT01;                                                            

   RETAIN   ;                                                          

   INFILE   HSMLOG;                                                    

   INPUT    @001    HSMLOG       $120. ;                               

   INDX1A = INDEX(HSMLOG,'FUNC=RECYCLE');                              

IF INDX1A GT 0 THEN DO;                                                

      PUT HSMLOG;                                                      

      TOVOL     = SUBSTR(HSMLOG,36,6);                                 

      FRVOL     = SUBSTR(HSMLOG,50,6);                                 

      STARTTIME = SUBSTR(HSMLOG,79,6);                                 

      ENDTIME   = SUBSTR(HSMLOG,91,6);                                 

      OUTPUT;                                                          

                    END;                                              

Super User
Super User
Posts: 7,994

Re: Read previous record after a condition

Your data appears to be named input.  Therefore use named input read:

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

E.g.

data want;

     infile "xyz.txt";

     input tod= date= name=$ func=$ toval=$ frvol=$ devt=$;

run;

You should then have the variable to process the data as you like.

Super User
Posts: 5,516

Re: Read previous record after a condition

Here's a way to make all the information available:

DATA OUT01;                                                       

      INFILE   HSMLOG;                                                   

   INPUT    @001    prior_HSMLOG       $120.  /

                 @001    HSMLOG               $120.;

   INDX1A = INDEX(HSMLOG,'FUNC=RECYCLE');                             

IF INDX1A GT 0 THEN DO;                  

...

END;

It assumes you are always working with 2 lines of raw data at a time.  If that's not the case, it gets a little more involved, but can still be done.  But let's only cross that bridge if necessary.  Good luck.

Good luck.

Ask a Question
Discussion stats
  • 2 replies
  • 196 views
  • 0 likes
  • 3 in conversation