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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

@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

16 REPLIES 16
Reeza
Super User
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.
luu
Fluorite | Level 6 luu
Fluorite | Level 6

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.

 

Astounding
PROC Star

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.

luu
Fluorite | Level 6 luu
Fluorite | Level 6

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.

 

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

love the %bquote

novinosrin
Tourmaline | Level 20

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;
Astounding
PROC Star

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.

luu
Fluorite | Level 6 luu
Fluorite | Level 6

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.

novinosrin
Tourmaline | Level 20

@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

luu
Fluorite | Level 6 luu
Fluorite | Level 6

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.

 

 

novinosrin
Tourmaline | Level 20

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

 

novinosrin
Tourmaline | Level 20

@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

luu
Fluorite | Level 6 luu
Fluorite | Level 6

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!

Astounding
PROC Star

In a DATA step, I would try:

 

test1 = "%superq(test)";

 

 

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
  • 16 replies
  • 4437 views
  • 2 likes
  • 5 in conversation