DATA Step, Macro, Functions and more

SAS Dates

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

SAS Dates


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;


Accepted Solutions
Solution
‎03-12-2015 12:28 PM
PROC Star
Posts: 7,474

Re: SAS Dates

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.

View solution in original post


All Replies
Solution
‎03-12-2015 12:28 PM
PROC Star
Posts: 7,474

Re: SAS Dates

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.

Valued Guide
Posts: 860

Re: SAS Dates

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;

Super User
Posts: 5,509

Re: SAS Dates

Posted in reply to Steelers_In_DC

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.

Contributor
Posts: 33

Re: SAS Dates

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!

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 282 views
  • 6 likes
  • 4 in conversation