Hey folks,
I get really frustrated working with SAS dates! I created a variable called SpringDLS that I want to call and use in a calculation in a data step. The macro variable is right, (02/11/2015), but I'm getting a syntax error. Any ideas?
libname GADS 'C:\TEMP\';
data _null_;
SpringDLS = '11-feb-2015'd;
call symput ('SpringDLS',trim(left(put(SpringDLS, mmddyy10.))));
run;
proc import
datafile = 'C:\Temp\GADS CM 02 Feb 2015.CSV'
out = gads1
DBMS = CSV replace;
run;
data gads2 (drop = Cause_detail Sub_System);
set gads1;
format &SpringDLS mmddyy10.;
StartTime = timepart(StartDateTime);
format StartTime HHMM.;
EndTime = timepart(EndDateTime);
format EndTime HHMM.;
StartDate = datepart(StartDateTime);
format StartDate mmddyy10.;
EndDate = datepart(EndDateTime);
format EndDate mmddyy10.;
NumDays = EndDate-StartDate + 1;
if (&SpringDLS >= StartDate and &SpringDLS <= EndDate) then DLS = 'Y';
if DLS = 'Y' then NumIntervals = NumDays*96-4;
else NumIntervals = NumDays*96;
run;
I'd start by changing the initial datastep, as you aren't getting a SAS date with your current code. Try:
data _null_;
SpringDLS = '11feb2015'd;
call symput ('SpringDLS',SpringDLS);
run;
Plus, I don't think you can use a format statement to assign a format to a macro variable (like &SpringDLS) and don't need it anyway.
I'd start by changing the initial datastep, as you aren't getting a SAS date with your current code. Try:
data _null_;
SpringDLS = '11feb2015'd;
call symput ('SpringDLS',SpringDLS);
run;
Plus, I don't think you can use a format statement to assign a format to a macro variable (like &SpringDLS) and don't need it anyway.
I broke it down a little different but get the same results:
%let date = '11feb2015';
data _null_;
call symput('SpringDLS',input(&date,date9.));
run;
data test;
format date2 mmddyy10.;
date1=&date;
date2=&SpringDLS + 1;
run;
Mark,
Regarding your original question, you have to picture the statemetn in your DATA step:
if (02/11/2015 >= StartDate and 02/11/2015 <= EndDate) then DLS = 'Y';
If you had coded this in a DATA step, the slahes would indicate division: 02 divided by 11, divided by 2015. In that light, perhaps it makes sense why you got the wrong result originally.
Similarly, picture this statement in a DATA step:
date1 = '11feb2015';
That would make DATE1 a character variable. If you want it to be a date, you would need to code:
date1 = '11feb2015'd;
In this case, that would mean:
date1 = &date.d;
The trick is to picture what the SAS code looks like, once macro language has helped generate the SAS statements.
Good luck.
Thanks guys! I'm sure I'll be back when I hit the next step... I'm going to try and struggle through it on my own 1st!
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.