BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
duanzongran
Obsidian | Level 7

dear all:
I want to write the macro variable a into datasets ,the code is :

 

*here a is a weird string,below is a example;
%let a="hello","world!";
data test;
x="'"||%nrstr(&a.)||"'";
run;

but this is failed ,the log shows :

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant,
a datetime constant, a missing value, arrayname, (, +, -, INPUT, NOT, PUT, ^, _NEW_, ~.

ERROR 200-322: The symbol is not recognized and will be ignored.

the expected result is :

data test_want;
x_want='"hello","world!"';
run;

How can I make the code work? OR a better way to write the weird string to dataset?

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data test;
x="%bquote(&a.)";
run;

 

You need to use quoting functions to handle a macro variable like &A which contains special characters, in this case the comma is the problem. You need double quotes around the macro function in order to have SAS recognize the results as a character string.

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
data test;
x="%bquote(&a.)";
run;

 

You need to use quoting functions to handle a macro variable like &A which contains special characters, in this case the comma is the problem. You need double quotes around the macro function in order to have SAS recognize the results as a character string.

--
Paige Miller
duanzongran
Obsidian | Level 7
Thank you very much for your patience and clear guidance!
duanzongran
Obsidian | Level 7

Thank you sir!

I guess  below is what you want to tell me:

x = quote(symget("a"),"'");

I didn't expect it to be so difficult to control the output of single and double quotes in SAS!

 

Tom
Super User Tom
Super User

What value do you want to put into the dataset variable?

Do you just want the value that is in the macro variable?

x = symget('a');

Do you want store the quoted value instead?  Then don't just append quotes, use the QUOTE() function as it will properly double up any embedded quotes.

419  %let a="hello","world!";
420  data test;
421    x=quote(symget('a'));
422    y=quote(symget('a'),"'");
423    put x= / y=;
424  run;

x="""hello"",""world!"""
y='"hello","world!"'

Or do you want all of the individually quotes words in your macro variable?  if the value is as nice as your example use it to generate a DO loop.

426  %let a="hello","world!";
427  data test;
428    length x $50;
429    do x=&a;
430      put x=;
431      output;
432    end;
433  run;

x=hello
x=world!
NOTE: The data set WORK.TEST has 2 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
duanzongran
Obsidian | Level 7
Thank you Tom! You enriched my imagination about the string "hello","world!"

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1263 views
  • 4 likes
  • 4 in conversation