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

Hi everyone, 

I have a dataset after I sorted by id date, results looks like below:

date   id     flag

JAN    1      0

FEB    1      0

JAN    2      1

FEB    2      1

JAN    3      0

FEB    3      1

 

I want to see:  first flag=1 group by id date. 

date   id     flag   flag1

JAN    1      0

FEB    1      0

JAN    2      1       1

FEB    2      1

JAN    3      0

FEB    3      1       1

 

My code failed:

data test;

set old;

by id date flag;

if first.flag=1 then flag1=1; 

 

anyone knows how to get data I want? Thank you so much! 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Try it this way:

 

data want;

set have;

by id;

retain dummy;

if first.id then dummy=flag;

else dummy + flag;

if dummy=1 then do;

   flag1=1;

   dummy=2;

end;

drop dummy;

run;

View solution in original post

11 REPLIES 11
novinosrin
Tourmaline | Level 20

are you missing

retain flag1;

jojozheng
Quartz | Level 8

no. I just want to denote every first flag=1 corrurance and let it be flag1=1. 

novinosrin
Tourmaline | Level 20

is this your want?

 

date   id     flag   flag1

JAN    1      0

FEB    1      0

JAN    2      1       1

FEB    2      1

JAN    3      0

FEB    3      1       1

jojozheng
Quartz | Level 8
yes
novinosrin
Tourmaline | Level 20
data have;
input date $   id     flag;
cards;
JAN    1      0
FEB    1      0
JAN    2      1
FEB    2      1
JAN    3      0
FEB    3      1
;

data want;

set have;

by id  flag notsorted ;

if first.flag and flag=1 then flag1=1; 
run;
Astounding
PROC Star

That's fine as long as you have a maximum of 2 months per ID.  Once you hit 3 months per ID, you have to account for a flag pattern like 1 0 1.

jojozheng
Quartz | Level 8
I have a dataset with million dataset, I just set an example for the situation I try to get.
jojozheng
Quartz | Level 8
Thank you @novinosrin
result from your code
date id flag flag1

JAN 1 0

FEB 1 0

JAN 2 1 1

FEB 2 1 1

JAN 3 0

FEB 3 1 1


but how to not retain 1 for every id, I just need first flag=1 to be denoted. Thanks!
Astounding
PROC Star

Try it this way:

 

data want;

set have;

by id;

retain dummy;

if first.id then dummy=flag;

else dummy + flag;

if dummy=1 then do;

   flag1=1;

   dummy=2;

end;

drop dummy;

run;

mkeintz
PROC Star

This one uses virtually the same logic as @Astounding.  I put it in for variety, and a possible unreasonable regard for compactness.

 

data have;
input date $   id     flag;
cards;
JAN    1      0
FEB    1      0
JAN    2      1
FEB    2      1
JAN    3      0
FEB    3      1
;

data want (drop=n_ones);
  set have;
  by id flag;

  if first.id then n_ones=0;
  n_ones+flag;
  flag1= ifn(flag=1 and n_ones=1,1,0);
run;    
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
jojozheng
Quartz | Level 8
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
  • 11 replies
  • 2012 views
  • 0 likes
  • 4 in conversation