DATA Step, Macro, Functions and more

Assign a date to a macro variable and use it to subset the data from a dataset

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Assign a date to a macro variable and use it to subset the data from a dataset

Hello Everyone

 

I am trying to assign a date to a macro variable and using that macro variable to subset a data from a dataset. It's throwing me an error. Here is the code:

 

/*Assigning the macro variable the 27th Jan 2018 date*/
%let date_comp= %SYSFUNC(INTNX(DAY,%SYSFUNC(TODAY()),-3),date9.);
%put &date_comp.;
27JAN2018

DATA want(where=( Ship_Date >=&date_comp.));
set have;
run;



The want data is throwing me an error. This is the error.

 

data want (where=(ship_date >=&date_comp.));
NOTE: Line generated by the macro variable "DATE_COMP".
1 27JAN2018
-------
22
76
ERROR: Syntax error while parsing WHERE clause.
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>,
=, >, >=, AND, EQ, GE, GT, LE, LT, NE, OR, ^=, |, ||, ~=.

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

3721 set have;
3722 run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

ERROR: Syntax error while parsing WHERE clause.

Please help me with this. 

 

Thanks

Chandan Mishra


Accepted Solutions
Solution
‎01-30-2018 10:19 AM
Respected Advisor
Posts: 2,662

Re: Assign a date to a macro variable and use it to subset the data from a dataset

[ Edited ]
Posted in reply to chandan_mishra
DATA want(where=( Ship_Date >="&date_comp"d));
set have;
run;

Formatted SAS dates which are text must be enclosed in single or double quotes, followed by the letter D (and since you are using a macro variable, it must be double quotes).

 

You can save yourself a bunch of coding by not formatting the macro variable as date9., just leave it as an integer.

--
Paige Miller

View solution in original post


All Replies
Solution
‎01-30-2018 10:19 AM
Respected Advisor
Posts: 2,662

Re: Assign a date to a macro variable and use it to subset the data from a dataset

[ Edited ]
Posted in reply to chandan_mishra
DATA want(where=( Ship_Date >="&date_comp"d));
set have;
run;

Formatted SAS dates which are text must be enclosed in single or double quotes, followed by the letter D (and since you are using a macro variable, it must be double quotes).

 

You can save yourself a bunch of coding by not formatting the macro variable as date9., just leave it as an integer.

--
Paige Miller
Super User
Super User
Posts: 9,227

Re: Assign a date to a macro variable and use it to subset the data from a dataset

Posted in reply to chandan_mishra
DATA want(where=( Ship_Date >= "&date_comp."d));

Is the update you need, however why bother at all, it does nothing more than:

data want;
  set have;
  where ship_date > today()-3;
runl

Except making the code far harder to read, maintain and debug.  KISS - Keep It Simple Smart - is a mantra you should as programmer live by!

Occasional Contributor
Posts: 18

Re: Assign a date to a macro variable and use it to subset the data from a dataset

Hello 

 

Thanks for your amazing reply. I am using a master file to run all the separate codes and want data is in the separate code. It's only because I don't have to change the code every time in the separate code, I used a macro variable in the master file and using it in the separate code. But I understood what you want to say. Thank you.

 

Thanks

Chandan Mishra

PROC Star
Posts: 8,117

Re: Assign a date to a macro variable and use it to subset the data from a dataset

Posted in reply to chandan_mishra

Try:

DATA want(where=( Ship_Date >="&date_comp."d));

The macro variable, itself, only contains text.

 

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 79 views
  • 3 likes
  • 4 in conversation