DATA Step, Macro, Functions and more

FIRST DOT AND LAST DOT LOGIC

Reply
Super Contributor
Posts: 1,041

FIRST DOT AND LAST DOT LOGIC

 

Hello,

i have a dataset "OLD" which has several records per ID and also some ID's are just once.

While using the below code , does SAS consider the one record per ID as the first record or the last record???

i am assuming that SAS would not consider it as the first or the last but would satisfy for first and last condition

To my surprise , using the below code single record per ID are being outptted which have time_elapse > 0, when i am thinking they should not

Could someone clarify for me please

 

 

 

data new ;
  set old ;
               by hsp_account_id recorded_time;
               retain outflag ;
               if first.hsp_account_id then outflag = 0 ;
               time_elapse = datepart(recorded_time) - dateprocedurekey2 ;
               if time_elapse ge 0 and outflag = 0 then do ;
                               first_amb = 1 ;
                              output ;
                              outflag=1 ;
               end ;                    
               if (first.hsp_account_id  and last.hsp_account_id) and time_elapse lt 0 then output ; 
                
 run ;  

Super User
Posts: 19,822

Re: FIRST DOT AND LAST DOT LOGIC

Posted in reply to robertrao
if time_elapse>0 and outflag=0 then your record is output - because you've specified so. Without any data beyond this it's impossible to say. The code can be 'correct' but not be the logical implementation you want.
Respected Advisor
Posts: 4,927

Re: FIRST DOT AND LAST DOT LOGIC

Posted in reply to robertrao

When there is a single record for a given hsp_account_id, first.hsp_account_id and last.hsp_account_id are both true.

PG
Super Contributor
Posts: 1,041

Re: FIRST DOT AND LAST DOT LOGIC

Posted in reply to robertrao
Hi PG. thanks for the reply. If I understand you correctly when there is a single record then would it qualify under both the codes shown below??
1) if first. Hsp_account_id
2) if first.hsp_account_id and last. Hsp_account_id
Respected Advisor
Posts: 4,927

Re: FIRST DOT AND LAST DOT LOGIC

Posted in reply to robertrao

Yes. But condition 2 is true ONLY if there is a single record.

PG
Super Contributor
Posts: 1,041

Re: FIRST DOT AND LAST DOT LOGIC

Posted in reply to robertrao
Sure.
Contrarily "If first.hsp_account_id "applies to both :
1)When multiple records per id are present and also
2) to a single record ....(part of it is satisfying here)

???

Respected Advisor
Posts: 4,927

Re: FIRST DOT AND LAST DOT LOGIC

Posted in reply to robertrao

When there are multiple records per id, if first.hsp_accound_id is true for the first record of the group. So if you look only at that condition, you don't know if the record is unique or the first one of a group.

PG
Super Contributor
Posts: 1,041

Re: FIRST DOT AND LAST DOT LOGIC

Hello,

 

I could not clearly understand this logic inspite of all your efforts.

Here below I have an example of having a single record in my "HAVE "dataset (no multiple records per ID and Recorded_Time).

I cant understand how this line is being outputted into "WANT" using the below code. Since it is the first and last record this part of the code if (first.id and last.id) and time_elapse lt 0 then output ; is implied based on my understanding and since time_elapse is greater than zero this record should never be outputted into WANT dataset...

also why is OUTFLAG set to ZERO for this record???

 

The main aim in this exercise here is to take the first recorded_Time ( in case of multiple records)

 

 

ID                    Recorded_Time   prcedure_date      outflag         time_elapse

101                05JUN2014              02JUN2014             0                   3

 

data WANT ;
  set HAVE ;
               by id recorded_time;
               retain outflag ;
               if first.id then outflag = 0 ;
               time_elapse = datepart(recorded_time) - procedure_date ;
               if time_elapse ge 0 and outflag = 0 then do ;
                               first_amb = 1 ;
                              output ;
                              outflag=1 ;
               end ;
               if (first.id  and last.id) and time_elapse lt 0 then output ;
                
 run ;

 

 

 

Super User
Posts: 5,431

Re: FIRST DOT AND LAST DOT LOGIC

Posted in reply to robertrao
Outflag is set to zero at line 5.
And the output is executed from the "time_elapse ge 0 and outflag = 0" condition block.
I you have problem understanding how variables are assigned values etc insert a couple of put _all_; statements in your program.
Data never sleeps
Super User
Super User
Posts: 7,060

Re: FIRST DOT AND LAST DOT LOGIC

[ Edited ]
Posted in reply to robertrao

If you want to treat the single records differently then perhaps you just need to re-order your conditions and include an ELSE clause?

 

data new ;
   set old ;
   by hsp_account_id recorded_time;
   retain outflag ;
   time_elapse = datepart(recorded_time) - dateprocedurekey2 ;
   if (first.hsp_account_id  and last.hsp_account_id) then do;
      if time_elapse lt 0 then output ;
   end;
   else do;
      if first.hsp_account_id then outflag = 0 ;
      if time_elapse ge 0 and outflag = 0 then do ;
         first_amb = 1 ;
         output ;
         outflag=1 ;
      end ;                    
   end;                
 run;
Ask a Question
Discussion stats
  • 9 replies
  • 498 views
  • 0 likes
  • 5 in conversation