BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hkim3677
Calcite | Level 5

Hi,

 

I want to isolate the first pre-and post observations. 

 

For example,

 

data example;

input id year event;

datalines;

a 1990 0

a 1991 0

a 1992 0

a 1993 1

a 1994 0 

a 1995 0

a 1996 1

a 1997 0

b 1993 0

b 1994 0

b 1995 1

b 1996 0

b 1997 1

b 1998 0

b 1999 0

;

run;

 

I want to get this dataset. like..

 

data hope;

input id year event;

datalines;

a 1990 0

a 1991 0

a 1992 0

a 1993 1

a 1994 0 

a 1995 0

 

b 1993 0

b 1994 0

b 1995 1

b 1996 0

 

;

run;

 

please help!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Something like this?

 

data want(drop=flag);
   set example;
   by id;
   if first.id then flag=0;
   if event=1 then flag+1;
   retain flag;
   if flag<2;
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

I don't follow the logic here. Please be more specific.

hkim3677
Calcite | Level 5
Each id has two events. So, I want to remove observations as of the second event year by id.
PeterClemmensen
Tourmaline | Level 20

Something like this?

 

data want(drop=flag);
   set example;
   by id;
   if first.id then flag=0;
   if event=1 then flag+1;
   retain flag;
   if flag<2;
run;
hkim3677
Calcite | Level 5
Thank you!
novinosrin
Tourmaline | Level 20
data example;

input id $ year event;

datalines;
a 1990 0
a 1991 0
a 1992 0
a 1993 1
a 1994 0 
a 1995 0
a 1996 1
a 1997 0
b 1993 0
b 1994 0
b 1995 1
b 1996 0
b 1997 1
b 1998 0
b 1999 0
;

run;

data want;
call missing(_e,__e);
do until(last.id);
set example;
by id year;
if  event and not _e then _e=1;
else if event and _e then __e=1;
if not __e then output;
end;
drop _:;
run;

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