Your SAS programs, embedded in web apps and elsewhere

string to sas date

Reply
N/A
Posts: 0

string to sas date

In my data I have a hire_date field which contains datetime. I used datepart to get the numeric value of the date. Now the user will enter begin_date which is a string ex. is 01/01/2006. I need to to convert the string to sas date so I want to select the records hire_date is greater than begin_date. I am having tough time to figure out
Can any one help me.
SAS Super FREQ
Posts: 8,742

Re: string to sas date

Hi,
Refer to my July 20 post in this forum prior posting:
http://support.sas.com/forums/thread.jspa?messageID=6918ᬆ

Your easiest solution (in my opinion) is to use a SAS date constant in your where clause. The form that a date constant has to take is:
[pre]
"ddmmmyyyy"d or "ddmmyy"d
[/pre]

That means the best solution would be to set your parameter to the DATE9 form of the date and then you don't have to worry about conversion.
[pre]

data btime;
infile datalines;
input name $ bdtime : datetime.;
bdtime2 = bdtime;
return;
datalines;
anna 15Nov1950:07:15:00
barb 15Nov1950:10:45:32
cali 15Nov1950:06:33:27
dora 15Nov1950:03:25:59
yora 15Nov1960:04:04:07
zora 15Nov1984:11:56:32
;
run;

proc print date=btime;
title 'How to get ONLY the people born after November 15, 1950';
where datepart(bdtime) gt "15Nov1950"d;
format bdtime2 datetime20.;
run;
[/pre]
If your &BEGIN_DATE was already in the form 15Nov1950, then you could just code the above WHERE clause as:
[pre]
where datepart(bdtime) gt "&BEGIN_DATE"d;
[/pre]

BUT, if your users need/want to select a different date format, then you'll need to convert YOUR macro parm "&BEGIN_DATE from one form into another date form. So your stored process will have to contain macro code to perform the conversion before the WHERE clause:

[pre]
%let begin_date = 11/15/1950; /* mimics what is coming from user */
%let datenum = %sysfunc(inputn(&begin_date,anydtdte10.));
%let wantdate = %sysfunc(putn(&datenum,date9.));

ods listing;
proc print date=btime;
title 'How to "fix" when date is not initially in right form for date constant';
where datepart(bdtime) gt "&wantdate"d;
format bdtime2 datetime20.;
run;
[/pre]

cynthia
N/A
Posts: 0

Re: string to sas date

Hi Cynthia,
I tried whatever u suggested, It worked. Thank you very much

Thanks lot
Ask a Question
Discussion stats
  • 2 replies
  • 328 views
  • 0 likes
  • 2 in conversation