DATA Step, Macro, Functions and more

Problems using %str

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

Problems using %str

Hi,

 

I am getting problems using %str.

 

When i do:

 

%let val = %str(data coches; set sashelp.cars; run);

%put val = &val.;

&val.;

it works properly, but now I make a modification:

 

%let fich = cars;

%let fich2 = %str(%")&fich%str(%");

%let val  = %str(data coches; set coches; fichero = &fich2 ;run;);

%put val = &val.;

&val.;

it doesn' work in the put stament I can see the macrovaroable well created

 %put val = &val.;
val = data coches; set coches; fichero = "cars" ;run;

but when I invoke tha macrovariable to execute de the data steps with errors:

 

23         &val.;
180: LINE y COLUMN no se pueden determinar.
NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 180-322: Statement is not valid or it is used out of proper order.

           _
           386
           200
ERROR 386-185: Expecting an arithmetic expression.

ERROR 200-322: The symbol is not recognized and will be ignored.

24         
25         GOPTIONS NOACCESSIBLE;
26         %LET _CLIENTTASKLABEL=;
27         %LET _CLIENTPROJECTPATH=;
28         %LET _CLIENTPROJECTNAME=;
29         %LET _SASPROGRAMFILE=;
30         
31         ;*';*";*/;quit;run;
                     ____
                     180

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.

 

Can anybody help me??

Thanks in advance


Accepted Solutions
Solution
‎09-15-2017 07:07 AM
Valued Guide
Posts: 558

Re: Problems using %str

Posted in reply to juanvg1972

You need to put double quotes around &fich2 in order to force resolution of the macro variable when val is assigned like so

 

%let fich = cars;

%let fich2 = %str(%")&fich%str(%");

%let val  = %str(data coches; set coches; fichero = "&fich2" ;run;);

%put val = &val.;

&val.;

View solution in original post


All Replies
Respected Advisor
Posts: 4,543

Re: Problems using %str

Posted in reply to juanvg1972

@juanvg1972

Try...

%put val = %unquote(&val.);

...and let us know if that helped.

Solution
‎09-15-2017 07:07 AM
Valued Guide
Posts: 558

Re: Problems using %str

Posted in reply to juanvg1972

You need to put double quotes around &fich2 in order to force resolution of the macro variable when val is assigned like so

 

%let fich = cars;

%let fich2 = %str(%")&fich%str(%");

%let val  = %str(data coches; set coches; fichero = "&fich2" ;run;);

%put val = &val.;

&val.;
Super User
Posts: 9,560

Re: Problems using %str

Posted in reply to juanvg1972

The masking functions add tokens to the strings that are recognized by the macro processor, but will confuse the main SAS interpreter.

Bottom line: don't add quotes in macro variables, put them around the macro variables in the data step. A situation where you don't know beforehand if a variable is character or numeric is strongly advised against. Such constructs will bite you in the behind as soon as you turn your back on them.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,537

Re: Problems using %str

[ Edited ]
Posted in reply to juanvg1972

You are complicating your program (and the possibility for errors) by adding pieces you don't need.  This statement would work without %STR (as long as you use double-quotes):

 

%let fich2 = "&fich";

 

In fact, you could skip creating &FICH2 entirely:

 

%let val = %str(data coches; set coches; fichero = "&fich"; run; ) ;

 

It's possible that this fixes everything.  But if it doesn't, I'm expecting that %PUT would still work.  Only the code execution needs to be changed.  If this is the final step:

 

&val

 

You may need to change it to:

 

%unquote(&val)

 

It's likely you won't need %UNQUOTE in this particular case (I can't test it right now), but might need it if you were generating SQL code instead of a DATA step.

 

 SIde note:  In the solution that you accepted, check the value of FICHERO.  I suspect it contains extra characters.

Super User
Super User
Posts: 9,200

Re: Problems using %str

Posted in reply to juanvg1972

Nobody questioning the bizarre use of Base SAS in a macro variable?  I would go mad if I saw that in any code I had to deal with.  Base SAS if the programming language, macro is only there to help generate some it, it is not for anything else.

Valued Guide
Posts: 558

Re: Problems using %str

I agree absolutely @RW9 - as it stands there's no reason to be doing what the OP posted and every reason not to do it that way. I took it as an exercise in understanding how macro variable resolution works. I too would be less than happy seeing something like that in production code.......

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 198 views
  • 3 likes
  • 6 in conversation