BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
2 REPLIES 2
Cynthia_sas
Diamond | Level 26
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
deleted_user
Not applicable
Hi Cynthia,
I tried whatever u suggested, It worked. Thank you very much

Thanks lot

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1406 views
  • 0 likes
  • 2 in conversation