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 ;
When there is a single record for a given hsp_account_id, first.hsp_account_id and last.hsp_account_id are both true.
Yes. But condition 2 is true ONLY if there is a single record.
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.
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 ;
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.