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

 Hi guys,

 

I have a quite simple problem, but I don't have a clue how can I solve it.

 

I have a macrovariable that contains quotes and ampersand (I need it for the excel file). Take a look at this code:

%macro x;

	/* ... */

	%let string = "This is sample string" & char(10) & "This is second line of sample string";
	%put &=string;

	data x;
		a=&string;
	run;

%mend;
%x;

But the problem is that I don't know how can I add such a string into data set (data x in my sample code). Does anyone have any idea?

 

 Thanks!
 Filip

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16
a=symget('string');
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

5 REPLIES 5
yabwon
Amethyst | Level 16
a=symget('string');
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



data_null__
Jade | Level 19

SYMGET

 

49         %let string = "This is sample string" & char(10) & "This is second line of sample string";
50         %put &=string;
STRING="This is sample string" & char(10) & "This is second line of sample string"
51         
52         data a;
53            x = symget('STRING');
54            put 'NOTE: ' x=;
55            run;

NOTE: x="This is sample string" & char(10) & "This is second line of sample string"
Kurt_Bremser
Super User

Use the SYMGET function:

data x;
a = symget("string");
run;

If you don't want a length of 200 for your new variable, use a LENGTH statement before the assignment.

filippo_kow
Obsidian | Level 7

 Hi all,

 

Thanks a lot for your replies, that works perfectly! 🙂

 

 Best,

 Filip

Quentin
Super User

An aleternative to using symget (a DATA step function  that will execute once per iteration of the loop), would be to use a macro quoting function, such as %superq, e.g.:

 

1    %let string = "This is sample string" & char(10) & "This is second line of sample string";
2    %put &=string;
STRING="This is sample string" & char(10) & "This is second line of sample string"
3
4    data x;
5      a="%superq(string)";
6      put a= ;
7    run;

a="This is sample string" & char(10) & "This is second line of sample string"
NOTE: The data set WORK.X has 1 observations and 1 variables.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 2098 views
  • 3 likes
  • 5 in conversation