I have a column in a data set that is
Type = Numeric
Length = 8
Format = MONYY7.
So the entries appear like FEB2021
APR2020
NOV2020
MAR2021
and so forth. How can I convert this data to
February 2021
April 2020
November 2020
March 2021
Thank you for assistance.
wklierman
So currently have date values (number of days since 1960) and are using the MONYY format to display them. You could use the WORDDATE format, but that will include the day of the month. Note also that will by default right align the values.
proc print data=have;
format datevar worddate.;
run;
There is also the NLDATE format that will produce similar values. That is left aligned but it also adjusts the strings to match your language settings.
If you really want to not print the day of the month then you will probably need to make your own format. Or create a new character variable to store that information.
data test;
length month datevar datevar2 8 datestr1 datestr2 $18;
do month=1 to 12;
datevar=mdy(month,1,2021);
datevar2=datevar;
datestr1=put(datevar,worddate18.-L);
datestr2=catx(' ',scan(datestr1,1),scan(datestr1,-1));
format datevar monyy7. datevar2 worddate.;
output;
end;
run;
Obs month datevar datevar2 datestr1 datestr2 1 1 JAN2021 January 1, 2021 January 1, 2021 January 2021 2 2 FEB2021 February 1, 2021 February 1, 2021 February 2021 3 3 MAR2021 March 1, 2021 March 1, 2021 March 2021 4 4 APR2021 April 1, 2021 April 1, 2021 April 2021 5 5 MAY2021 May 1, 2021 May 1, 2021 May 2021 6 6 JUN2021 June 1, 2021 June 1, 2021 June 2021 7 7 JUL2021 July 1, 2021 July 1, 2021 July 2021 8 8 AUG2021 August 1, 2021 August 1, 2021 August 2021 9 9 SEP2021 September 1, 2021 September 1, 2021 September 2021 10 10 OCT2021 October 1, 2021 October 1, 2021 October 2021 11 11 NOV2021 November 1, 2021 November 1, 2021 November 2021 12 12 DEC2021 December 1, 2021 December 1, 2021 December 2021
Hello,
The closest you can come is with the NLDATEw. format.
NLDATEw. is similar to DATEw. and WORDDATEw., except that NLDATEw. is locale-specific.
Locale-specific means this:
options locale=English_UnitedStates;
data test;
day=15760;
put day nldate.;
run;
versus (for example)
options locale=German_Germany;
data test;
day=15760;
put day nldate.;
run;
You can also make your own format of course with PROC FORMAT!
Koen
is your variable a SAS date?
Just make a new variable.
data lab;
set bios511.rawlab;
newdate=strip(put(labdate,monname10.))||''||put(year(labdate),4.);
keep labdate newdate;
run;
So currently have date values (number of days since 1960) and are using the MONYY format to display them. You could use the WORDDATE format, but that will include the day of the month. Note also that will by default right align the values.
proc print data=have;
format datevar worddate.;
run;
There is also the NLDATE format that will produce similar values. That is left aligned but it also adjusts the strings to match your language settings.
If you really want to not print the day of the month then you will probably need to make your own format. Or create a new character variable to store that information.
data test;
length month datevar datevar2 8 datestr1 datestr2 $18;
do month=1 to 12;
datevar=mdy(month,1,2021);
datevar2=datevar;
datestr1=put(datevar,worddate18.-L);
datestr2=catx(' ',scan(datestr1,1),scan(datestr1,-1));
format datevar monyy7. datevar2 worddate.;
output;
end;
run;
Obs month datevar datevar2 datestr1 datestr2 1 1 JAN2021 January 1, 2021 January 1, 2021 January 2021 2 2 FEB2021 February 1, 2021 February 1, 2021 February 2021 3 3 MAR2021 March 1, 2021 March 1, 2021 March 2021 4 4 APR2021 April 1, 2021 April 1, 2021 April 2021 5 5 MAY2021 May 1, 2021 May 1, 2021 May 2021 6 6 JUN2021 June 1, 2021 June 1, 2021 June 2021 7 7 JUL2021 July 1, 2021 July 1, 2021 July 2021 8 8 AUG2021 August 1, 2021 August 1, 2021 August 2021 9 9 SEP2021 September 1, 2021 September 1, 2021 September 2021 10 10 OCT2021 October 1, 2021 October 1, 2021 October 2021 11 11 NOV2021 November 1, 2021 November 1, 2021 November 2021 12 12 DEC2021 December 1, 2021 December 1, 2021 December 2021
There is no specific format, however you can create your own format or use monname. format and year function.
data have;
length dat $20.;
test_date=today();
format test_date monyy7.;
dat=upcase(put(test_date,monname.)||" "||put(year(test_date),4.));
run;
proc print data=have; run;
haha I just posted the same code!
Thank you for the coding approach.
I appreciate your help.
wklierman
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.