01-28-2015 10:44 AM
I have a date variable called sDate in my data set that is defined as Type=Number Length=8 Format=DATETIME20.
example value is 05AUG2012:00:00:00
I want to use sDate as the basis for creating another variable that displays the sDate as 05-AUG-12
data temp2 ;
length zDate $9 ;
set temp1 ;
zDate = put(sDate, date9.) ;
zDate within my data set is all asterisks.
Thank you for any help
01-28-2015 10:55 AM
sDate is a numeric SAS date time variable, which is the number of seconds since 01 Jan 1960.
You can either convert that to a numeric SAS date variable, which is the number of days since 01 Jan 1960,
or you can convert it to a character date variable.
Above, you are creating a character date variable, but you are missing the proper date function to extract a date from the date time variable.
zDate = put(datepart(sDate),date9.);
This will create a character date that you can store or display.
If you want to create a numeric SAS Date variable, you can just do
zDate = datepart(sDate);
Now it's a numeric SAS Date variable that you can add or subtract.
If you want to display it, you can format it using any of the SAS date formats.
Format zDate date9.;
01-28-2015 11:15 AM
Thank you so much Hai.Kuo and burntDirt.
That worked but the format is 05AUG2012. How can I change that to 05-AUG-12?
Thank you so much.
01-28-2015 12:12 PM
Thank you for your reply. That almost worked. It now returns 05-AUG-2012. I need the year to only be 2 characters.
01-28-2015 12:45 PM
Then you have to make your format.
01-28-2015 12:58 PM
Slight modification to accommodate years such as 2001 to display as 01 and dates 5 to display as 05
low-high = '%0d-%b-%0y'
format date yymonddd.;
do date='01Jan2014'd to '31Jan2014'd;
01-28-2015 01:17 PM
After having to go through a lot of Y2K issues I prefer to always use 4-digit years and any of my projects has to justify the use of 2-digits. At least with SAS that information is available and it is just how you are choosing to display it.
01-28-2015 12:47 PM
I can't think of a date format that will give you that exactly.
But, if you're making a character SAS date, you can always edit it.
substr(zDate,8,2) = '';
zDate = compress(zDate);
There are probably a billion more ways to do this, but this was the first that popped in my head.
01-28-2015 01:38 PM
Thank you everyone for all your help. I spoke with my lead and we are going to keep it with 4 digits for the year. He always says no whenever I ask to change things but this time he agreed it is a better way to go.