DATA Step, Macro, Functions and more

Calculating an interval from today's date

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Calculating an interval from today's date

Hi all, newbie question:

 

I currently have a huge dataset listing thousands of patients and the dates they've had blood drawn on.  I'd like to winnow the list down to just the bloods drawn during the week before last to create a less cumbersome working list for those draws.  

 

I found the Today function while poking around online and figured that I could just use that to grab today's date and calculate an interval from that but it isn't working as expected.  My code attempt:

 

data

data _null_;
tday=today();

 

proc sql;
create table Bloods_Abridged as select distinct patient_number, date_drawn from Bloods_Complete 
where (tday-date_drawn) < 17 and (tday-date_drawn) > 11; quit;

 

And the resulting error text:

 

ERROR: The following columns were not found in the contributing tables: tday.

 

So clearly I'm messing something up in getting the table to recognize 'tday' but I have no idea what.  Also not 100% sure if my code for calculating the interval is correct, since the problem with the table not recognizing 'tday' means I don't even get there. The date_drawn column is in mmddyy10 format, would my code for the interval work if the initialization of today's date worked?  Trying this on SAS 9.4.  Any help is appreciated, thanks!


Accepted Solutions
Solution
‎02-15-2017 06:19 PM
Trusted Advisor
Posts: 1,022

Re: Calculating an interval from today's date

Posted in reply to sentientcarbon

Because your "data data _null_" statement created a dataset named "data", with 1 var and 1 observation.  You never called that dataset in proc sql, and it wouldn't have been very useful anyhow.

 

Why not use the "today()" function, instead of variable TDAY,  inside your proc sql?

View solution in original post


All Replies
Solution
‎02-15-2017 06:19 PM
Trusted Advisor
Posts: 1,022

Re: Calculating an interval from today's date

Posted in reply to sentientcarbon

Because your "data data _null_" statement created a dataset named "data", with 1 var and 1 observation.  You never called that dataset in proc sql, and it wouldn't have been very useful anyhow.

 

Why not use the "today()" function, instead of variable TDAY,  inside your proc sql?

New Contributor
Posts: 3

Re: Calculating an interval from today's date

Aaaaaaaand that worked perfectly, thanks!  Just deleted the data step and changed the proc step to this:

 

proc sql; 

create table Bloods_Abridged as select distinct patient_number, date_drawn from Bloods_Complete 
where (today()-date_drawn) < 17 and (today()-date_drawn) > 11; quit;

 

And it gave me exactly what I wanted.  The weird initial format was from me copying the Today function code straight off the support.sas site thinking I shouldn't mess with it since I pretty much only know enough SAS to break stuff Smiley Happy

 

Thanks again!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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