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

I have a data set like following

data have;
informat date1 date9.;
format date1 date9.;
input date1 id flag$;
cards;
21jan2018 1 n
21jan2018 1 y
21jan2018 1 y
22jan2018 1 y
22jan2018 1 y
22jan2018 1 y
22jan2018 2 n
22jan2018 2 y
22jan2018 2 y
22jan2018 2 y
22jan2018 2 y
;
run;

I want to add a flag1 such that same date and same id flag1 will take value of flag and retain that value for same date and id.

once id got change in same date then flag 1 should have value of flag and retain that value for same date and id

once date gets changes again code should assign value of flag to flag1 and retain that value for date and id

I want an output like following

Date ID Flag Flag1

21jan2018 1 n n
21jan2018 1 y n
21jan2018 1 y n
22jan2018 1 y y
22jan2018 1 y y
22jan2018 1 y y
22jan2018 2 n n
22jan2018 2 y n
22jan2018 2 y n
22jan2018 2 y n
22jan2018 2 y n

I tried using following code but not successful.


data test1;
set test;
by date1 id flag;
retain flag1;
if first.date1  then do;
if first.id then do;
if flag ne "n" then flag1 = "y";
else if flag eq "n" then flag1 = "n";
end;
end;
run;

please help

 

Regards,

VR

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

All you need is:

 

data test1;
set test;
by date1 id;
retain flag1;
if first.id then flag1 = flag;
run;

first.id is = 1 whenever date1 OR id changes.

 

PG

View solution in original post

2 REPLIES 2
PGStats
Opal | Level 21

All you need is:

 

data test1;
set test;
by date1 id;
retain flag1;
if first.id then flag1 = flag;
run;

first.id is = 1 whenever date1 OR id changes.

 

PG

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
  • 2 replies
  • 11895 views
  • 0 likes
  • 2 in conversation