I would like to create a new variable that take YYMMn6 format to display yearmonth, taking value from another numeric variable.
For example, the original numeric variable has value 199312, the new variable would display the same value "199312".
So far I have tried:
format new_yearmonth = put(yearmonth, YYMMn6.);
* but this take yearmonth value as date value and convert this to a weird number 245603 or something;
I have successfully converted something similar to yearquarter format with the code:
format yearquarter YYQn6.;
yearquarter = YYQ(year,quarter);
*where year is a numeric variable having value 1993, quarter is also a numeric variable that has value 1, 2, 3, and 4;
So I guess I can also try this method but could not find a function similar to YYQ that takes value of 2 variable year and month. Is there such function?
thanks
You need first to convert the input numeric variable to a sas date variable.
Sas date contains: year, month and day.
Having input like var = 201706 - the day is absent. You can choose any day to adjust to the input.
In such cases ususally we adjust day=01;
There are many ways to convert your input to a sas date.
In order to display the sas date as yyyymm assign to it the format YYMMN6.
Format new_date yymmn6.;
1) using MDY function - needs to split your input to year and month:
new_date = mdy(mod(var,100), 01, int(var/100) );
/* ------- month day year -------*/
2) alternatively, convert the number given to a string and adjust the day:
new_date = input(put(var,6.),yymmdd8.);
3) The most simple way to convert the input to a sas date - which maybe you used is:
new_date = input(put(var,6.), yymmn6.);
format new_date yymmn6.;
You need first to convert the input numeric variable to a sas date variable.
Sas date contains: year, month and day.
Having input like var = 201706 - the day is absent. You can choose any day to adjust to the input.
In such cases ususally we adjust day=01;
There are many ways to convert your input to a sas date.
In order to display the sas date as yyyymm assign to it the format YYMMN6.
Format new_date yymmn6.;
1) using MDY function - needs to split your input to year and month:
new_date = mdy(mod(var,100), 01, int(var/100) );
/* ------- month day year -------*/
2) alternatively, convert the number given to a string and adjust the day:
new_date = input(put(var,6.),yymmdd8.);
3) The most simple way to convert the input to a sas date - which maybe you used is:
new_date = input(put(var,6.), yymmn6.);
format new_date yymmn6.;
Thank you so much. it works perfectly.
Just a quick question. How do we compare the date? for example, if I want dates after 199702? Since it is not numeric, I cannot simply do >199702.
Thanks
Sas date counts days since 01/01/1960 = 0;
If you convert the two input variables to date it is just simple numeric comparison.
Anyway, as your char-date is in yyyymm format, you can compare them as
alphanumeric strings. If it was in mmyyyy format it will give false results.
YEAR4. format is applicable with a sas date varibale.
If your variable as a numeric year only (used with BEST4. format) then
convert it with MDY function, adding any month or day and assign the YEAR4. format.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.