I have a data value in the format date9. which I am converting to yymmn6. and need to subtract 1 month from the yymmn6. value and tried below code. Can someone please correct where we need correction in the code. As I am getting unexpected values under old_dt column
data two; set want; new_dt = input(put(active_dt,yymmn6.),6.); old_dt = intnx('month',new_dt,-1); run;
What format have you assigned to the Old_dt variable?
Personally if Active_dt is a date value this should be:
data two; set want; old_dt = intnx('month',active_dt,-1); format old_dt yymmn6. ; run;
When you have a date value no reason to recalculate just change the format for some step you need a different appearance.
Any code that looks like Input (put(datevariable, format.), informat.) is a waste of coding effort.
What format have you assigned to the Old_dt variable?
Personally if Active_dt is a date value this should be:
data two; set want; old_dt = intnx('month',active_dt,-1); format old_dt yymmn6. ; run;
When you have a date value no reason to recalculate just change the format for some step you need a different appearance.
Any code that looks like Input (put(datevariable, format.), informat.) is a waste of coding effort.
Why not just leave them as DATE values and attach format to print the values in that YYYYMM style?
data two;
set want;
new_dt = active_dt ;
old_dt = intnx('month',active_dt,-1);
format new_dt old_dt yymmn6.;
run;
If you really need to create those integers in the YYY,YMM style then remember that the INTNX() interval MONTH works with DATE values. Since you seem to have a date valued variable (otherwise the DATE9 format would print the values as gibberish) just use that variable with your INTNX() call.
data two;
set want;
new_dt = input(put(active_dt,yymmn6.),6.);
old_dt = input(put(intnx('month',active_dt,-1),yymmn6.),6.);
run;
If you only have the YYY,YMM integers then convert them into date values first.
Example:
185 data test; 186 new_dt=202403; 187 date_value=input(put(new_dt,z6.),yymmn6.); 188 format date_value yymmdd10.; 189 put (_all_) (=); 190 run; new_dt=202403 date_value=2024-03-01
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.