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.
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
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?
@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 .
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
@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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.