BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,

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,
Mark
1 REPLY 1
Cynthia_sas
SAS Super FREQ
Hi:
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:
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#/documentation/cdl/...

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

[pre]
sasdate = input(DATE,ddmmyy10.);
[/pre]

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

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:
http://support.sas.com/forums/thread.jspa?threadID=7550

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:
http://support.sas.com/forums/thread.jspa?threadID=10311
http://support.sas.com/forums/thread.jspa?threadID=5558
http://support.sas.com/forums/thread.jspa?threadID=7963

and this documentation link is always useful
http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#/documentation/cdl/...

and these papers are also useful:
http://www2.sas.com/proceedings/sugi24/Coders/p073-24.pdf
http://www2.sas.com/proceedings/sugi30/255-30.pdf
http://www2.sas.com/proceedings/sugi27/p101-27.pdf

cynthia

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 1 reply
  • 659 views
  • 0 likes
  • 2 in conversation