single quote in macro variable

Accepted Solution Solved
Reply
Occasional Contributor luu
Occasional Contributor
Posts: 15
Accepted Solution

single quote in macro variable

There is a single quote in macro variable test. How could i assign macro variable test to test1 in data set test? Thanks!

 

%let test=%str(antidepressant -can't remember name);
data test;
test1=&test;
run;


Accepted Solutions
Solution
‎04-09-2018 06:32 PM
PROC Star
Posts: 1,836

Re: single quote in macro variable

@luu Ok I think you have a point here as my successive tests kinda gave me crazy log messages. Why bother. Just immediately quote it before the datastep like this:

 

data xxx;

input  medicine_name $50.;
datalines;
levothyroxin 100mgcg
antidepressant -can't remember name
Simvastatin Tabs
gabapentin 300mg
;
%macro luu;

proc sql noprint;

        select medicine_name into : test1-:test99999 from xxx;

quit;

  %do i=1 %to &sqlobs;
  %let test&i=%bquote(&&test&i);
  %end;


/*And then assign macro variable into a dataset variable:*/

data result;

med="&test2";

run;

%mend ulu;

%luu

This is a safe bet. HTH

View solution in original post


All Replies
Super User
Posts: 23,773

Re: single quote in macro variable

Try the following:
test1 = "&test";

You need quotes anyways, to store it as a character variable, otherwise you're saying:

test1=antidpressant;

Which is looking for a variable called antidepressant, not the value of antidepressant.
Occasional Contributor luu
Occasional Contributor
Posts: 15

Re: single quote in macro variable

Thank you for your reply. I write it as  test1="&test" but still has error. It seems the single quote in macro variable test cannot be resolved well.

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>,
=, >, ><, >=, AND, EQ, GE, GT, LE, LT, MAX, MIN, NE, NG, NL, OR, ^=, |, ||, ~=.

ERROR 388-185: Expecting an arithmetic operator.

 

Super User
Posts: 6,785

Re: single quote in macro variable

You would use the same method whether or not there is a single quote:

 


data test;
test1="&test";
run;

 

The only time you run into a problem is when &TEST contains doublequotes.

Occasional Contributor luu
Occasional Contributor
Posts: 15

Re: single quote in macro variable

Posted in reply to Astounding

Thank you for your reply. I write it as  test1="&test" but still has error. It seems the single quote in macro variable test cannot be resolved well.

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>,
=, >, ><, >=, AND, EQ, GE, GT, LE, LT, MAX, MIN, NE, NG, NL, OR, ^=, |, ||, ~=.

ERROR 388-185: Expecting an arithmetic operator.

 

PROC Star
Posts: 1,836

Re: single quote in macro variable

%let test=%bquote(antidepressant -can't remember name);
%put &test;
data test;
test1="&test";
run;

love the %bquote

PROC Star
Posts: 1,836

Re: single quote in macro variable

Posted in reply to novinosrin

or wanna keep and play with %str then precede the unmatched quote with % like the following:

%let test=%str(antidepressant -can%'t remember name);
%put &test;
data test;
test1="&test";
run;
Super User
Posts: 6,785

Re: single quote in macro variable

Posted in reply to novinosrin

As indicated, getting quotes into and out of a macro variable can be tricky.  I would go with %STR in this case.  For experimentation and learning, here's a program you can run and try to explain:

 

data _null_;

call symputx('p', "This won't work");

run;

 

%put &p;

%put &p;

 

DON'T do this in the middle of an interactive session.  (Unless you figure out how the program works, you would need to shut down the session and start over.)  Use a background job.

Occasional Contributor luu
Occasional Contributor
Posts: 15

Re: single quote in macro variable

Posted in reply to novinosrin

Thank you very much! That works! But do you know how to only change test1="&test" writing instead of change the define of &test?

Because i simplied the code for the question. In my original code the &test variable is the value of a column of a dataset not the variable using %let defined.

PROC Star
Posts: 1,836

Re: single quote in macro variable

[ Edited ]

@luuwrote:

In my original code the &test variable is the value of a column of a dataset not the variable using %let defined.


In that case you don't need the double quotes wrapper

 

PS we do not know your data, so we can only assume and  guess. HTH

So give me a sample of the data please

Occasional Contributor luu
Occasional Contributor
Posts: 15

Re: single quote in macro variable

Posted in reply to novinosrin

Thank you for your great help! 

 

Part of my data is like this, there is a variable named "medicine_name"

 

   medicine_name

levothyroxin 100mgcg

antidepressant -can't remember name

Simvastatin Tabs

gabapentin 300mg

 

 

I put them into a list of macro vairables:

 

proc sql noprint;

        select medicine_name into : test1-:test99999 from xxx;

quit;

 

And then assign macro variable into a dataset variable:

data result;

med="&test2";

run;

 

Because &test2 is "antidepressant -can't remember name", at this point, i get the error.

 

 

PROC Star
Posts: 1,836

Re: single quote in macro variable

[ Edited ]

Your code seems to work fine when i tested it:

data xxx;

input  medicine_name $50.;
datalines;
levothyroxin 100mgcg
antidepressant -can't remember name
Simvastatin Tabs
gabapentin 300mg
;

proc sql noprint;

        select medicine_name into : test1-:test99999 from xxx;

quit;

 

/*And then assign macro variable into a dataset variable:*/

data result;

med="&test2";

run;

 

Here is the log:


228 data xxx;
229
230 input medicine_name $50.;
231 datalines;

NOTE: The data set WORK.XXX has 4 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


236 ;
237
238 proc sql noprint;
239
240 select medicine_name into : test1-:test99999 from xxx;
241
242 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds


243
244
245
246 /*And then assign macro variable into a dataset variable:*/
247
248 data result;
249
250 med="&test2";
251
252 run;

NOTE: The data set WORK.RESULT has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

 

Solution
‎04-09-2018 06:32 PM
PROC Star
Posts: 1,836

Re: single quote in macro variable

@luu Ok I think you have a point here as my successive tests kinda gave me crazy log messages. Why bother. Just immediately quote it before the datastep like this:

 

data xxx;

input  medicine_name $50.;
datalines;
levothyroxin 100mgcg
antidepressant -can't remember name
Simvastatin Tabs
gabapentin 300mg
;
%macro luu;

proc sql noprint;

        select medicine_name into : test1-:test99999 from xxx;

quit;

  %do i=1 %to &sqlobs;
  %let test&i=%bquote(&&test&i);
  %end;


/*And then assign macro variable into a dataset variable:*/

data result;

med="&test2";

run;

%mend ulu;

%luu

This is a safe bet. HTH

Occasional Contributor luu
Occasional Contributor
Posts: 15

Re: single quote in macro variable

Posted in reply to novinosrin

You are right, this program can run through. I notice there are some other places I used &test2 I have modified it. Thank you very much for your great help!

Super User
Posts: 6,785

Re: single quote in macro variable

In a DATA step, I would try:

 

test1 = "%superq(test)";

 

 

☑ This topic is solved.

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

Discussion stats
  • 16 replies
  • 246 views
  • 2 likes
  • 5 in conversation