BookmarkSubscribeRSS Feed
robertrao
Quartz | Level 8

 

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 ;  

9 REPLIES 9
Reeza
Super User
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.
PGStats
Opal | Level 21

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
robertrao
Quartz | Level 8
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
PGStats
Opal | Level 21

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

PG
robertrao
Quartz | Level 8
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)

???

PGStats
Opal | Level 21

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
robertrao
Quartz | Level 8

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 ;

 

 

 

LinusH
Tourmaline | Level 20
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
Tom
Super User Tom
Super User

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;

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
  • 9 replies
  • 2018 views
  • 0 likes
  • 5 in conversation