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

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