BookmarkSubscribeRSS Feed
pobel
Calcite | Level 5

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, ^, ^=, |,

              ||, ~, ~=

5 REPLIES 5
RichardinOz
Quartz | Level 8

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

pobel
Calcite | Level 5

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);

art297
Opal | Level 21

I think your answer is because %str masks some of the characters you used, namely (, ) and blank.  Take a look at:

http://www2.sas.com/proceedings/sugi24/Advtutor/p38-24.pdf

RichardinOz
Quartz | Level 8

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

pobel
Calcite | Level 5

Thank you, Art! 

Thanks again, Richard!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 2889 views
  • 6 likes
  • 3 in conversation