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
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.;
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.;
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.
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.
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.
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.......
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.