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;
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;
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.