BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
msyteriouspages
Calcite | Level 5
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
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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

 

View solution in original post

1 REPLY 1
Reeza
Super User
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

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 636 views
  • 0 likes
  • 2 in conversation