BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
juanvg1972
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

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

6 REPLIES 6
Patrick
Opal | Level 21

@juanvg1972

Try...

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

...and let us know if that helped.

ChrisBrooks
Ammonite | Level 13

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.;
Kurt_Bremser
Super User

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.

Astounding
PROC Star

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

ChrisBrooks
Ammonite | Level 13

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