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;
@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
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.
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.
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.
%let test=%bquote(antidepressant -can't remember name);
%put &test;
data test;
test1="&test";
run;
love the %bquote
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;
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.
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.
@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
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.
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
@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
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!
In a DATA step, I would try:
test1 = "%superq(test)";
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.