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

Deal Experts. 

 

I have the following quest. 

Default_date variable should be updated with the first instance of "Default" for the Status variable, and thereafter it has to have the same date until It changes back to "no def" for the Status variable. I think the following example can explain better.

Have_table

IDDateStatus
A01/01/2000No def
A02/01/2000Default
A03/01/2000Default
A04/01/2000No def
A05/01/2000Default
A06/01/2000Default
B01/01/2000Default
B02/01/2000Default
B03/01/2000No def
B04/01/2000No Def
B05/01/2000Default
B06/01/2000No Default

 

Want_table

IDDateStatusDefault_Date
A01/01/2000No def.
A02/01/2000Default02/01/2000
A03/01/2000Default02/01/2000
A04/01/2000No def.
A05/01/2000Default05/01/2000
A06/01/2000Default05/01/2000
B01/01/2000Default01/01/2000
B02/01/2000Default01/01/2000
B03/01/2000No def.
B04/01/2000No Def.
B05/01/2000Default05/01/2000
B06/01/2000No Default.

 

Thank you so much for your help and time.

 

Regards,

Myu

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data want;
 set have;
 by id  status notsorted;
 retain Default_Date;
 if first.id or status ne 'Default' then Default_Date=.;
 if first.status and status='Default' then Default_Date=date;
run;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

data want;
 set have;
 by id  status notsorted;
 retain Default_Date;
 if first.id or status ne 'Default' then Default_Date=.;
 if first.status and status='Default' then Default_Date=date;
run;
Myurathan
Quartz | Level 8
@novinosrin, Again, Thank you so much for your help.
Myurathan
Quartz | Level 8

@novinosrin  If I want to have the defaut_date as follows, how should I change the code? 

 

IDDateStatusDefault_Date
A01/01/2000No def.
A02/01/2000Default02/01/2000
A03/01/2000Default02/01/2000
A04/01/2000No def02/01/2000
A05/01/2000Default05/01/2000
A06/01/2000Default05/01/2000
B01/01/2000Default01/01/2000
B02/01/2000Default01/01/2000
B03/01/2000No def01/01/2000
B04/01/2000No Def01/01/2000
B05/01/2000Default05/01/2000
B06/01/2000No Def05/01/2000

 

Defult_date variable will have the date when is the last date status moved from No Def to Default.

 

Thanks in advance.

novinosrin
Tourmaline | Level 20

Hi @Myurathan , Please try the below and let me know-


data have;
input ID $	Date :mmddyy10.	Status & $;*	Default_Date;
cards;
A	01/01/2000	No def	 .
A	02/01/2000	Default	02/01/2000
A	03/01/2000	Default	02/01/2000
A	04/01/2000	No def	 02/01/2000
A	05/01/2000	Default	05/01/2000
A	06/01/2000	Default	05/01/2000
B	01/01/2000	Default	01/01/2000
B	02/01/2000	Default	01/01/2000
B	03/01/2000	No def	 01/01/2000
B	04/01/2000	No Def	 01/01/2000
B	05/01/2000	Default	05/01/2000
B	06/01/2000	No Def	 05/01/2000
;

data want;
 set have;
 by id Status notsorted;
 retain Default_Date;
 if first.id then Default_Date=.;
 if first.status and status='Default' then Default_Date=date;
 format Default_Date mmddyy10.;
run;

Myurathan
Quartz | Level 8
@novinosrin It works like charms. Thank you so much.
cminard
Obsidian | Level 7

data one;
input
@1 id $2.
@3 date mmddyy10.
@14 status $10.
;
format date mmddyy10.;
datalines;
A 01/01/2000 No def
A 02/01/2000 Default
A 03/01/2000 Default
A 04/01/2000 No def
A 05/01/2000 Default
A 06/01/2000 Default
B 01/01/2000 Default
B 02/01/2000 Default
B 03/01/2000 No def
B 04/01/2000 No Def
B 05/01/2000 Default
B 06/01/2000 No Default
;
run;

proc print data=one;
run;

data two;
set one;
by id date;
format default_date mmddyy10.;
retain default_date;
if first.id then do;
if status in ('No def','No Default') then default_date=.;
if status='Default' then default_date=date;
end;
else do;
if status in ('No def','No Default') then default_date=.;
if status='Default' then default_date=date;
end;
run;

proc print date=two;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 738 views
  • 5 likes
  • 3 in conversation