Help using Base SAS procedures

Date between

Posts: 0

Date between


I want to write a code to add a variable say 'Month' such that if the Date is between 1/10/2002 to 31/10/2009 then month = month1 and if Date is between 1/11/2002 to 30/11/2009 then month = month2; if the Date is between 1/12/2001 to 31/11/2009 then month = month3.

could you suggest please.

Date is character.

Kind regards,
Posts: 8,743

Re: Date between

As previously explained, it is hard to do comparisons with dates that are character strings. Your best bet is to turn your character DATE variable into a SAS date value and then use DATA step logic to create your new variables.

You say you want:
month = month1;
month = month2;
month = month3;

but, could you want:

month = 'month1';
month = 'month2';
month = 'month3';

Are MONTH1, MONTH2 and MONTH3 variables or are you trying to create a character variable MONTH, whose values will be the text strings "MONTH1", "MONTH2" and "MONTH3"??? It makes a difference in the code whether MONTH will be character or numeric and whether MONTH1, MONTH2 and MONTH3 are variables or the values you want to assign.

As explained previously (in talking about TIME) the INPUT function turns a character string (such as a character date or a character time) into a numeric value...this assumes that your INFORMAT is the correct INFORMAT for your data. It looks like you are showing a comparison value in the form dd/mm/yyyy. There is a DDMMYY informat, as described in the documentation:

so, you should be able to turn your character DATE into a SAS date, with syntax similar to this:

sasdate = input(DATE,ddmmyy10.);

And then you should be able to simply do this, utilizing the MONTH function (and assigning a text string to the MONTH variable):
if month(SASDATE) = 10 then month = "month1";
else if month(SASDATE) = 11 then month = "month2";
else if month(SASDATE) = 12 then month = "month3";

Note that I did NOT use your full date values in the comparison. Why not? Your ranges and logical conditions do NOT make sense to me.

Your first range is from Oct 1, 2002 thru October 31, 2009 and then your second range is from Nov 1, 2002 thru Nov 30, 2009. And then your THIRD range is from Dec 1, 2001 to Nov 31, 2009 (Nov 31 is an invalid date). But Nov 1, 2002 would fall into the first range and a date of Nov 29, 2009 would fall into the second range -AND- the third range. And, since the ending date for your third range is an invalid date (31/11/2009), some further clarification is needed on your part about what you ACTUALLY want and what your date ranges are.

As previously explained, if I wanted to test whether a particular SAS date variable value needed to be compared to a SPECIFIC date value, I would have to provide a DATE constant or a numeric value for date for purposes of the comparison.

You will get more (and more on-point) help, if you can post the code that you've tried and show the error messages or explain the problems you've encountered. There is a previous forum posting that explains how to post code that contains < and > symbols or <= and >= symbols. That forum posting is here:

Many previous forum postings have dealt with the question of how to handle character date values and character time values and turn them into SAS dates using the INPUT function. Searching the forums for that information should help you, if you are unwilling to post your code. A few of those previous forums are listed here for your convenience:

and this documentation link is always useful

and these papers are also useful:

Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation