Hi,
I run into an error message when using %str() to a macro variable value which conatins a dot "." in CASE--WHEN. here is the code:
data test;
do dt=.,"14NOV2012"d;
output;
end;
run;
%let mvar=%str(case dt
when . then " "
else put(dt,yymmdd10.)
end as dtc);
proc sql;
create table test1 as
select distinct &mvar
from test;
quit;
if we change the %LET statement as follows, it would be fine:
%let mvar=case dt
when . then " "
else put(dt,yymmdd10.)
end as dtc;
%let mvar=%quote(case dt
when . then " "
else put(dt,yymmdd10.)
end as dtc);
%let mvar=%quote(case dt
when . then " "
else put(dt,yymmdd10.)
end as dtc);
I know that the %STR() is not necessary here, but still want to know why there was ERROR message.
Thanks a lot in advance!
Best regards!
The error message for the first PROC SQL step is:
NOTE: Line generated by the macro variable "MVAR".
1 case dt when . then " " else put(dt,yymmdd10.) end as dtc
- ----
22 76
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,
a missing value, (, +, -, BTRIM, CALCULATED, CASE, EXISTS, INPUT, LOWER, NOT, PUT, SELECT, SUBSTRING, TRANSLATE,
UPPER, USER, ^, ~.
ERROR 76-322: Syntax error, statement will be ignored.
----
202
ERROR 202-322: The option or parameter is not recognized and will be ignored.
1 ! case dt when . then " " else put(dt,yymmdd10.) end as dtc
----
22
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, >=, ?, AND, BETWEEN,
CONTAINS, EQ, EQT, GE, GET, GT, GTT, IN, IS, LE, LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, THEN, ^, ^=, |,
||, ~, ~=
I can't replicate your error. However I sometimes find it is necessary to %UNQUOTE() a macro variable string such as yours. You could try
proc sql;
create table test1 as
select distinct %UNQUOTE(&mvar)
from test;
quit;
Richard in Oz
Hi Richard,
%UNQUOTE() works here. thanks!
There would be no ERROR if we put another %STR() around the dot '.', still cannot figure out why.
%let mvar=%str(case dt
when %str(.) then " "
else put(dt,yymmdd10.)
end as dtc);
I think your answer is because %str masks some of the characters you used, namely (, ) and blank. Take a look at:
As Art points out, %str() masks certain characters, which in practice means that although the character "looks" the same in the log it has a different underlying binary value. SAS Macro quoting can be a bit of a mystery and you often have to experiment a bit (as you did) to get the result you want.
Richard in Oz
Thank you, Art!
Thanks again, Richard!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.