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

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 428 views
  • 0 likes
  • 2 in conversation