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

Hello,

 

I need your help to find what I want.

 

My dataset is the following:

 

Person

Company

Year

Flag

A

aa

2005

1

A

bb

2007

0

A

bb

2008

0

B

B

yy

uu

2010

2011

0

1

B

C

cc

ee

2012

1998

1

0

C

ee

2002

1

C

ee

2015

0

C

pp

2016

0

C

pp

2017

1

 

From this set, I want to extract the subset behind which includes the observations after flag = 1 for each person. 

 

Person

Company

Year

Flag

A

aa

2005

1

A

bb

2007

0

A

bb

2008

0

B

uu

2011

1

B

cc

2012

1

C

ee

2002

1

C

ee

2015

0

C

pp

2016

0

C

pp

2017

1

 

If you know the easy way to solve the problem, please let me know.

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input Person $ Company $ Year Flag;
datalines;
A aa 2005 1 
A bb 2007 0 
A bb 2008 0 
B yy 2010 0
B uu 2011 1
B cc 2012 1 
C ee 1998 0
C ee 2002 1 
C ee 2015 0
C pp 2016 0
C pp 2017 1
;

data want;
   set have;
   by Person;
   if first.Person then _iorc_=0;
   if flag=1 then _iorc_=1;
   if _iorc_;
run;

Result:

 

Person Company Year Flag 
A      aa      2005 1 
A      bb      2007 0 
A      bb      2008 0 
B      uu      2011 1 
B      cc      2012 1 
C      ee      2002 1 
C      ee      2015 0 
C      pp      2016 0 
C      pp      2017 1 

View solution in original post

7 REPLIES 7
cool1993
Fluorite | Level 6

OH. Thanks. When I upload this file, the invalid HTML error has occurred so that the data is messed up. Now, it is revised. Please check it.

ChrisNZ
Tourmaline | Level 20

 

> the invalid HTML error has occurred 

1. Please always check that your question makes sense before submitting, rather than depending on others to do it.

 

2. Since you supply no code, we can't modify it.

Adding this to your data step should perform the action sought.

Modify as needed to suit your data.

  by PERSON;                    %* Tells SAS that the data is sorted;
  if first.PERSON then KEEP=0;  %* Because the data is sorted, we can initialise the KEEP flag;
  if FLAG=1 then KEEP+1;        %* Set the KEEP flag as soon as value 1 encountered;
  if KEEP;                      %* Use the KEEP flag to filter output data;

 

3. If you want fuller help, supply a working program and data (and check it that it works *as others see it*).

 

PeterClemmensen
Tourmaline | Level 20

I don't understand this logic. Why are the two obs with flag=1 included for Person=B, but not for Person=C

cool1993
Fluorite | Level 6

I want to make a set which, by person, has the observations after first.flag.

 

For example, in case of person B, the observation 5 (year 2011) and 6 (year 2012) are left in the set since the first.flage is 2011

PeterClemmensen
Tourmaline | Level 20
data have;
input Person $ Company $ Year Flag;
datalines;
A aa 2005 1 
A bb 2007 0 
A bb 2008 0 
B yy 2010 0
B uu 2011 1
B cc 2012 1 
C ee 1998 0
C ee 2002 1 
C ee 2015 0
C pp 2016 0
C pp 2017 1
;

data want;
   set have;
   by Person;
   if first.Person then _iorc_=0;
   if flag=1 then _iorc_=1;
   if _iorc_;
run;

Result:

 

Person Company Year Flag 
A      aa      2005 1 
A      bb      2007 0 
A      bb      2008 0 
B      uu      2011 1 
B      cc      2012 1 
C      ee      2002 1 
C      ee      2015 0 
C      pp      2016 0 
C      pp      2017 1 
cool1993
Fluorite | Level 6

WOW!! Thank you for your help.  

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