DATA Step, Macro, Functions and more

About an error message when using %str( )

Reply
Contributor
Posts: 23

About an error message when using %str( )

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

              ||, ~, ~=

Super Contributor
Posts: 644

Re: About an error message when using %str( )

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

Contributor
Posts: 23

Re: About an error message when using %str( )

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

PROC Star
Posts: 7,357

Re: About an error message when using %str( )

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

Super Contributor
Posts: 644

Re: About an error message when using %str( )

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

Contributor
Posts: 23

Re: About an error message when using %str( )

Thank you, Art! 

Thanks again, Richard!

Ask a Question
Discussion stats
  • 5 replies
  • 1633 views
  • 6 likes
  • 3 in conversation