BookmarkSubscribeRSS Feed
DB_ECON
Calcite | Level 5
I have a panel data with to identifiers: ID and Time. The dataset contains the following variables: ID, Time and V1. I want to create a fourth variable Flag which will identify the first date where V1 > 0 for each ID. For eg: The third observations identifies the third month as the first occurrence of a positive V1 for ID = 1. Again the seventh observation identifies the second month as the first occurrence of a positive V1 for ID = 2 . Thanks in advance.


ID Time V1 Flag
1 , 1, 0, 0
1 , 2, 0, 0
1 , 3, 7.5, 3
1 , 4, 6, 0
1 , 5, 0, 0
2 , 1, 0, 0
2 , 2, 9, 2
2 , 3, 9, 0
2 , 4, 0, 0
2 , 5, 2.5, 0
4 REPLIES 4
Reeza
Super User
Use retain with by groups and a found flag to indicate if the variable has already been set.
[pre]
data want;
set have;
by id;

retain found;

if first.id then found=0;

flag=0;

if v1 ne 0 and found=0 then do;
flag=time;
found=1;
end;

drop found;
run;
[/pre]
DB_ECON
Calcite | Level 5
Thanks a lot Reeza.....This works great!!
Ksharp
Super User
Hi.
If your data look like you post (e.g have been sorted).


[pre]

data temp;
infile datalines dlm=' ,';
input ID Time V1;
cards;
1 , 1, 0
1 , 2, 0
1 , 3, 7.5
1 , 4, 6
1 , 5, 0
2 , 1, 0
2 , 2, 9
2 , 3, 9
2 , 4, 0
2 , 5, 2.5
;
run;
data temp(drop=_id);
set temp;
retain _id .;
if id ne _id and v1 gt 0 then do;
_id=id;
flag=time;
end;
else flag=0;
run;
[/pre]






Ksharp

Message was edited by: Ksharp Message was edited by: Ksharp
DB_ECON
Calcite | Level 5
Thanks a lot to Ksharp as well.....This code also does the job....
Thanks Guys!!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 8550 views
  • 0 likes
  • 3 in conversation