BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
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.
1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

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

2 REPLIES 2
Peter_C
Rhodochrosite | Level 12
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
Cynthia_sas
SAS Super FREQ

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 59603 views
  • 4 likes
  • 3 in conversation