SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
wlierman
Lapis Lazuli | Level 10

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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 

 

View solution in original post

8 REPLIES 8
sbxkoenk
SAS Super FREQ

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

tarheel13
Rhodochrosite | Level 12

is your variable a SAS date?

wlierman
Lapis Lazuli | Level 10
It shows up in the format MONYY8. AUG2021
DEC2020

and so on
tarheel13
Rhodochrosite | Level 12

Just make a new variable. 

data lab;
	set bios511.rawlab;
	newdate=strip(put(labdate,monname10.))||''||put(year(labdate),4.);
	keep labdate newdate;
run;
Tom
Super User Tom
Super User

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 

 

Rydhm
Obsidian | Level 7

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;

Capture.PNG

tarheel13
Rhodochrosite | Level 12

haha I just posted the same code!

wlierman
Lapis Lazuli | Level 10

Thank you for the coding approach.

I appreciate your help.

 

wklierman

sas-innovate-white.png

Our biggest data and AI event of the year.

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.

 

Register now!

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
  • 8 replies
  • 14870 views
  • 3 likes
  • 5 in conversation