- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'm trying to use IF statements based upon the new MONTH values in my table, since I need to do the same thing based upon the month of the date and not the specific date itself (of which I have about 100,000 observations).
I've tried several things like:
IF date='3', then x=y, etc., but SAS won't read my IF statement based on my MONTH values I created. How do I get SAS to do this, as going back through the individual dates would be extremely time consuming task.
Thanks for any help you can give.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
if month( your_date_variable ) = 3 then do;
* do March processing ;
end;
If your_date_variable GT '31Dec2004'd then do;
* process cases after 2004 ;
end;
[/pre]
Does that help?
Even after you format a SAS date variable it is still a number of days since 1960. That is why SAS supports the date constants like '31Dec2004'd.
The MONTH() function is a convenient way of extracting just that information from a SAS date variable.
PeterC
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;