Hi:
The format only affects the DISPLAY of the date variable -- not the internal storage value of the date variable. So, for example, if I have a date variable called DATE that represents the date 11/15/1950, the INTERNAL, stored number for that date is -3334. DATE is a numeric variable. I can format DATE anyway I want for PROC PRINT or PROC FREQ. But, applying a MONTH format does not affect the internal storage of the number. And, it doesn't turn DATE into a character variable.
So, if I needed to use DATE in an IF statement, these are all acceptable IF statements:
if date = -3334 then x=y;
if date = '15NOV1950'd then x=y;
if month(date) = 11 then x=y;
This last IF statement uses the MONTH function with the DATE variable to return a number from 1 to 12 -- which represents only the MONTH portion of my DATE variable value of -3334.
cynthia
To check out different DATE internal numbers and how a format only DISPLAYs a readable date, test this code by putting your date of interest between the single quotes where I have 15NOV1950 -- note that you absolutely need the quotes and the d -- to tell SAS that you're creating your numeric variable from a DATE constant:
data mydate;
mydate = '15NOV1950'd;
date = mydate;
run;
ods listing;
proc print data=mydate;
title 'what is the internal number for a date';
format DATE mmmddyy10.;
run;
Hi:
The format only affects the DISPLAY of the date variable -- not the internal storage value of the date variable. So, for example, if I have a date variable called DATE that represents the date 11/15/1950, the INTERNAL, stored number for that date is -3334. DATE is a numeric variable. I can format DATE anyway I want for PROC PRINT or PROC FREQ. But, applying a MONTH format does not affect the internal storage of the number. And, it doesn't turn DATE into a character variable.
So, if I needed to use DATE in an IF statement, these are all acceptable IF statements:
if date = -3334 then x=y;
if date = '15NOV1950'd then x=y;
if month(date) = 11 then x=y;
This last IF statement uses the MONTH function with the DATE variable to return a number from 1 to 12 -- which represents only the MONTH portion of my DATE variable value of -3334.
cynthia
To check out different DATE internal numbers and how a format only DISPLAYs a readable date, test this code by putting your date of interest between the single quotes where I have 15NOV1950 -- note that you absolutely need the quotes and the d -- to tell SAS that you're creating your numeric variable from a DATE constant:
data mydate;
mydate = '15NOV1950'd;
date = mydate;
run;
ods listing;
proc print data=mydate;
title 'what is the internal number for a date';
format DATE mmmddyy10.;
run;
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!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.