BookmarkSubscribeRSS Feed
scolitti1
Calcite | Level 5

I need to create an automated report with a moving date. One that goes back a few weeks to present (with a 3 day lag).

 

I created a macro for the date here:

%let start =  %sysfunc(intnx(day,%sysfunc(today()),-22), date9.);
%let end =  %sysfunc(intnx(day,%sysfunc(today()),-3), date9.);

 

The goal is to create a line graph that will only show data between the 'start' and 'end' variables. Code for graph:


proc sgplot data=nine noborder;
where date >= '&start'd and date <= '&end'd;
vline date / response=admit_ma y2axis lineattrs=(color=blue thickness=4)
DATALABELPOS=data name='c';
xaxis discreteorder=data display=(noline nolabel noticks) fitpolicy=thin;
y2axis display=(noline noticks) grid label='New COVID Hospital Admissions' values=(0 to 1.0 by 0.1) offsetmin=0;
keylegend 'c'/ title='' linelength=24 noborder;
label admit_ma='Number of new admissions';
run;

 

I know this probably isn't the best way, but I am modeling after an old code that was being used. The error I get is based off the date variables:

ERROR: Invalid date/time/datetime constant '&start'd.

ERROR: Invalid date/time/datetime constant '&end'd.

Error: Syntax error while parsing WHERE clause.

2 REPLIES 2
ballardw
Super User

First point, for actual comparison values do not create text values, just use the date numeric and

that will solve the second: macro variables do not resolve inside single quotes.

 

Unless you have a need to display the date as text like 01MAY2023 somewhere else in the code you would be better off with:

 

%let start =  %sysfunc(intnx(day,%sysfunc(today()),-22));
%let end =  %sysfunc(intnx(day,%sysfunc(today()),-3));


proc sgplot data=nine noborder;
where date >=&start. and date <= &end.;
vline date / response=admit_ma y2axis lineattrs=(color=blue thickness=4)
DATALABELPOS=data name='c';
xaxis discreteorder=data display=(noline nolabel noticks) fitpolicy=thin;
y2axis display=(noline noticks) grid label='New COVID Hospital Admissions' values=(0 to 1.0 by 0.1) offsetmin=0;
keylegend 'c'/ title='' linelength=24 noborder;
label admit_ma='Number of new admissions';
run;

Since SAS does allow compound comparisons I typically would use this since it is a bit easier to follow intent

 

where &start. <= date <= &end.;
scolitti1
Calcite | Level 5

scolitti1_0-1683666071620.png

I implemented your suggestions. Did not seem to work

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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