BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to create a new data set based on current data set ttt.

I want to create a condition  that IF value of UntilYYMM is 31DEC2020 then  the value will be one month after FromYYMM value.

However in wanted data set the value in last row was not changed as I wanted and it is still 31DEC2020

 

data ttt;
format  FromYYMM date9. UntilYYMM  date9.;
input month FromYYMM :date9. UntilYYMM :date9. ;
cards;
2003 15Feb2020 13MAR2020
2004 14MAR2020 11APR2020
2005 12APR2020 09MAY2020
2006 10MAY2020 31DEC2020
;
run;
data wanted(drop=UntilYYMM rename=(UntilYYMM_new=UntilYYMM));
retain month FromYYMM UntilYYMM;
set ttt;
if last.month then UntilYYMM_new=intnx('month',FromYYMM,1);
else UntilYYMM_new=UntilYYMM;
format UntilYYMM_new date9.;
run;
2 REPLIES 2
Jagadishkatam
Amethyst | Level 16

Please try the below code

 

data wanted(drop=UntilYYMM rename=(UntilYYMM_new=UntilYYMM));
retain month FromYYMM UntilYYMM;
set ttt;
UntilYYMM_new=ifn(UntilYYMM='31DEC2020'd,intnx('month',FromYYMM,1),UntilYYMM);
format UntilYYMM_new date9.;
run;
Thanks,
Jag
Kurt_Bremser
Super User

Maxim 2: Read the Log.

It will alert you about your incorrect use of last.month without a

by month;

statement.

 


@Ronein wrote:

Hello

I want to create a new data set based on current data set ttt.

I want to create a condition  that IF value of UntilYYMM is 31DEC2020 then  the value will be one month after FromYYMM value.

However in wanted data set the value in last row was not changed as I wanted and it is still 31DEC2020

 

data ttt;
format  FromYYMM date9. UntilYYMM  date9.;
input month FromYYMM :date9. UntilYYMM :date9. ;
cards;
2003 15Feb2020 13MAR2020
2004 14MAR2020 11APR2020
2005 12APR2020 09MAY2020
2006 10MAY2020 31DEC2020
;
run;
data wanted(drop=UntilYYMM rename=(UntilYYMM_new=UntilYYMM));
retain month FromYYMM UntilYYMM;
set ttt;
if last.month then UntilYYMM_new=intnx('month',FromYYMM,1);
else UntilYYMM_new=UntilYYMM;
format UntilYYMM_new date9.;
run;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 721 views
  • 0 likes
  • 3 in conversation