Hello,
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.) ;
run ;
zDate within my data set is all asterisks.
Thank you for any help
data temp2 ;
length zDate $9 ;
set temp1 ;
zDate = put(datepart(sDate), date9.) ;
run ;
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.;
Good luck!
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.
DATE11. instead of DATE9.
bllardw,
Thank you for your reply. That almost worked. It now returns 05-AUG-2012. I need the year to only be 2 characters.
Thank you
Then you have to make your format.
proc format;
picture dttest
low-high='%d-%b-%y'
(datatype=date);
run;
data have;
a='01jan2015 00:00:00'dt;
b=put(datepart(a), dttest9.);
run;
Slight modification to accommodate years such as 2001 to display as 01 and dates 5 to display as 05
proc format;
picture yymonddd
low-high = '%0d-%b-%0y'
(datatype=date);
run;
data have;
format date yymonddd.;
do date='01Jan2014'd to '31Jan2014'd;
output;
end;
run;
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.
Hi Jerry,
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.
Try using
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.
Gary
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.
Thank you
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.