data want;
set have;
by ID;
retain first_found;
flag = 0;
if first.id then first_found=0;
if action = 'Test A' and first_found = 0 then do;
flag=1;
first_found=1;
end;
run;
Use another flag that is retained that checks if the first record was ever found.
@msyteriouspages wrote:
Hi there! I’m trying to create indicator values using two variables. I would like to get the first observation (of interest) according to the first time that observation occurs. For example, this is the dataset I have:
ID Action DateTime
1 Started 01AUG2021:15:39:01.000
1 Test A 01AUG2021:15:41:01.000
1 Check 01AUG2021:16:32:01.000
1 Test A 01AUG2021:16:39:01.000
1 Test B 01AUG2021:17:55:01.000
What I want:
I want to create an indicator that records the first time Test A happens
ID Action DateTime Test_A
1 Started 01AUG2021:15:39:01.000 0
1 Test A 01AUG2021:15:41:01.000 1
1 Check 01AUG2021:16:32:01.000 0
1 Test A 01AUG2021:16:39:01.000 0
1 Test B 01AUG2021:17:55:01.000 0
I tried multiple things, I tried to create a condition using minimum date & also by the first occurrence but nothing worked. I’d greatly appreciate any help. Thank you
data want;
set have;
by ID;
retain first_found;
flag = 0;
if first.id then first_found=0;
if action = 'Test A' and first_found = 0 then do;
flag=1;
first_found=1;
end;
run;
Use another flag that is retained that checks if the first record was ever found.
@msyteriouspages wrote:
Hi there! I’m trying to create indicator values using two variables. I would like to get the first observation (of interest) according to the first time that observation occurs. For example, this is the dataset I have:
ID Action DateTime
1 Started 01AUG2021:15:39:01.000
1 Test A 01AUG2021:15:41:01.000
1 Check 01AUG2021:16:32:01.000
1 Test A 01AUG2021:16:39:01.000
1 Test B 01AUG2021:17:55:01.000
What I want:
I want to create an indicator that records the first time Test A happens
ID Action DateTime Test_A
1 Started 01AUG2021:15:39:01.000 0
1 Test A 01AUG2021:15:41:01.000 1
1 Check 01AUG2021:16:32:01.000 0
1 Test A 01AUG2021:16:39:01.000 0
1 Test B 01AUG2021:17:55:01.000 0
I tried multiple things, I tried to create a condition using minimum date & also by the first occurrence but nothing worked. I’d greatly appreciate any help. Thank you
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.