Help using Base SAS procedures

Dates and IF statements

Accepted Solution Solved
Reply
N/A
Posts: 0
Accepted Solution

Dates and IF statements

I'm working with a list of dates from a 20-year period. The dates were in SAS format (from 1/1/60) and I've formatted them with the MONTHw. format.

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
Solution
‎07-24-2017 08:21 AM
SAS Super FREQ
Posts: 8,868

Re: Dates and IF statements

[ Edited ]
Posted in reply to deleted_user

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;

View solution in original post


All Replies
Valued Guide
Posts: 2,177

Re: Dates and IF statements

Posted in reply to deleted_user
two alternatives[pre]
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
Solution
‎07-24-2017 08:21 AM
SAS Super FREQ
Posts: 8,868

Re: Dates and IF statements

[ Edited ]
Posted in reply to deleted_user

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;
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 7949 views
  • 1 like
  • 3 in conversation