DATA Step, Macro, Functions and more

macro variable in a DATA Step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

macro variable in a DATA Step

[ Edited ]

I am trying to build Condition using some macro variables. tf_condition is the macro variable that holds the condition. Below is from the log.

 

tf_condition: UPCASE(tools) IN ("POWER TOOL1","POWER TOOL2","POWER TOOL3")

 

Then I use in a DATA STEP to filter the data.

 

DATA tools;

   SET tools_all;

  WHERE &tf_condition.;

RUN;

 

Below is the error:

 

WHERE &tf_condition;
NOTE: Line generated by the macro variable "TF_CONDITION".
30 UPCASE(tools) IN ("POWER TOOL1","POWER TOOL2","POWER TOOL3")
                                    _  _
                                   22 76
ERROR: Syntax error while parsing WHERE clause.
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant,
a missing value, -.

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

31 RUN;

 

If I ran the DATA step substituting value of tf_condition, then it runs fine.

 

I don't understand where is the problem.  Please help.


Accepted Solutions
Solution
‎10-19-2017 03:06 PM
Trusted Advisor
Posts: 1,934

Re: macro variable in a DATA Step

Try 

 

WHERE %unquote(&tf_condition);

View solution in original post


All Replies
Solution
‎10-19-2017 03:06 PM
Trusted Advisor
Posts: 1,934

Re: macro variable in a DATA Step

Try 

 

WHERE %unquote(&tf_condition);
Occasional Contributor
Posts: 19

Re: macro variable in a DATA Step

Posted in reply to PaigeMiller

Thank You. It works. Just curious, what's the prob here ?

Trusted Advisor
Posts: 1,934

Re: macro variable in a DATA Step

What is the problem?

 

Well, I can't really explain it, all I know is that when I see this error, %UNQUOTE fixes it. Perhaps Mr. @Astounding can explain it, he wrote a book about this stuff.

Super User
Posts: 5,518

Re: macro variable in a DATA Step

Posted in reply to PaigeMiller

The problem lies in the creation of the macro variable.  It must have been created using macro quoting functions as part of the code, which means that there are extraneous characters inserted into the value of the macro variable.  Usually, the software figures out what to do, which means that macro language removes those extraneous characters before passing the SAS statements to the SAS language processor.  But sometimes macro language gets it wrong and doesn't figure it out in time to correctly parse the characters within the macro variable.  Most notorious for having this flaw:  PROC SQL, and WHERE statements.  If you were to use this plain vanilla assignment (with no macro quoting functions), there should be no problem with referring to the macro variable:

 

%let tf_condition = UPCASE(tools) IN ("POWER TOOL1","POWER TOOL2","POWER TOOL3") ;

 

DATA tools;

   SET tools_all;

  WHERE &tf_condition.;

RUN;

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 144 views
  • 0 likes
  • 3 in conversation