BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Barkat
Pyrite | Level 9

I was trying to extract month using the following statements but all months display as Jan1960.

Month=month(Datepart(AdmitDate));
Format Month MONYY7.;

 

FYI: AdmitDate is a datetime variable. When I extract only date from AdmitDate using Datepart(AdmitDate), it works completely fine.

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

You don't need to pull the month out of the date value, just format the datepart. See example below, note I used admitDateTime to store the datetime value and admitDate to store the date value. Whereas your code uses admitDate and month.


 

2362  data _null_ ;
2363      admitDateTime=datetime() ;
2364      admitDate=datepart(admitDateTime) ;
2365      format admitDate monyy7. ;
2366      put admitDateTime= admitDate= ;
2367  run ;

admitDateTime=1954152114.8 admitDate=DEC2021
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Month=month(Datepart(AdmitDate));
Format Month MONYY7.;

 

The MONTH() function returns a number from 1 to 12, and so variable MONTH is a number from 1 to 12. So when you format the value of Month as MONYY7, the results will indeed be JAN1960. So what are you really trying to do? What is the desired output?

 

Does formatting AdmitDate as DTMONYY7. do what you want?

--
Paige Miller
Barkat
Pyrite | Level 9
I would like to get Jan2021
PaigeMiller
Diamond | Level 26

@Barkat wrote:
I would like to get Jan2021

The format DTMONYY. on your date/time variable should get you there. No need to create additional variables .

--
Paige Miller
AMSAS
SAS Super FREQ

You don't need to pull the month out of the date value, just format the datepart. See example below, note I used admitDateTime to store the datetime value and admitDate to store the date value. Whereas your code uses admitDate and month.


 

2362  data _null_ ;
2363      admitDateTime=datetime() ;
2364      admitDate=datepart(admitDateTime) ;
2365      format admitDate monyy7. ;
2366      put admitDateTime= admitDate= ;
2367  run ;

admitDateTime=1954152114.8 admitDate=DEC2021
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

 

PaigeMiller
Diamond | Level 26

@AMSAS wrote:

You don't need to pull the month out of the date value, just format the datepart. See example below, note I used admitDateTime to store the datetime value and admitDate to store the date value. Whereas your code uses admitDate and month.


You don't even need a new variable. Use format DTMONYY7. on the date/time variable.

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 482 views
  • 4 likes
  • 3 in conversation