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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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