简体   繁体   中英

How to extract month from a date in SAS

I am trying to extract a month from a date in SAS, but so far all my new month variables are coming up as missing.

I have attempted to use some combinations of the month() function in SAS, but so far it just comes up as missing. The dates are formatted as follows: 01/31/2017 (MMDDYY10.)

I have tried

month = month(end_date) 

Month =catx('/',put(month(end_date),z2

I would like the Month to show up as a number (01) or a 3 letter code (JAN), currently it is just missing (.)

Thanks in advance!

For month() to return a missing value the end_date variable must be numeric and missing. If end_date were a character variable the log would show invalid numeric data .

Use the monname3. format to convert a date value to a $3. character value mon

monthname = put (end_date, monname3.);

Other alternatives are:

  • keep the date value unchanged and change the format, or
  • map the date value to the first of the month value and also format that

For example:

end_date_copy = end_date;
format end_date_copy monname3.;

end_date_month = intnx('month', end_date, 0);
format end_date_month monname3.;

What you ultimately do depends on how the mon is to be used downstream in reporting or aggregating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM