BookmarkSubscribeRSS Feed
archana
Fluorite | Level 6

Old query

 

Data x ;

set y;

where Offer_St_dt >= '01OCT2011'd;

run;

I am trying to replace the above query with

New Query

 

%let year1 = %STR(%')%sysfunc(intnx(month,%sysfunc(today()),-12),date9.)%STR(%');

Data x ;

set y;

where Offer_St_dt >= &year1;

run;

I am getting error in the above case as

 

ERROR: Syntax error while parsing WHERE clause.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, a missing value, (, +, -, :, INPUT, NOT, PUT, ^, ~.

ERROR 76-322: Syntax error, statement will be ignored.

6 REPLIES 6
slchen
Lapis Lazuli | Level 10

Change to:

%let year1 =%sysfunc(intnx(month,%sysfunc(today()),-12),date9.);


archana
Fluorite | Level 6

It is not working. i am getting the same error.

Quentin
Super User

In your macro approach, you forgot the d at the end of the date literal, and you need to manually %unquote() the macro variable (this is an old macro language bug, it should unquote it for you but sometime it doesn't... when code from MPRINT looks right but fails, try %unquote().   Ksharp's non macro approach works for me as well.  Code below.

%let year1 = %STR(%')%sysfunc(intnx(month,%sysfunc(today()),-12),date9.)%STR(%')d; 

data have;
  Offer_St_dt="01Jan2012"d; output;
  Offer_St_dt="01Jan2014"d; output;
run;

%put &year1;

data want ;
  set have;
  where Offer_St_dt >= %unquote(&year1);
run; 

data want2 ;
  set have;
  where Offer_St_dt >=intnx('month',today(),-12)   ;
run;
BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Ksharp
Super User

Why not write it in data step?

Data x ;

set y;

where Offer_St_dt >=intnx('month',today(),-12)   ;

run;

jakarman
Barite | Level 11

A more easier approach would be setting some global macro variables by a sas-datastep.
When this is a standard approach it could be a standard setting part of sas start.

By that you can verify the content of the macro var. Leave it as text  and put the quotes and d indication when needed

---->-- ja karman --<-----
Tom
Super User Tom
Super User

Why are you using single quotes instead of double quotes?  Macro references do not resolve inside of single quotes.

%let year1 = "%sysfunc(intnx(month,%sysfunc(today()),-12),date9.)"d ;

Data x ;

  set y;

  where Offer_St_dt >= &year1;

run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 4187 views
  • 6 likes
  • 6 in conversation