BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I have a macro varaible called My1 with values X Y Z

I want to create a new macro variable called My2  based on My1 macro variable that will get values  'X'  'Y'   'Z'

 

%let My1=X Y Z;
/**Want to calculate a new macro var called My2 get values 'X'  'Y'  'Z'

 

 

6 REPLIES 6
Kurt_Bremser
Super User

Use a DATA step (simpler code).

Define a maximum length (32767) character variable.

Proceed through the macro variable (COUNTW and SCAN functions), use the QUOTE function to add the single quotes, and CATX to store the results in the new variable.

CALL SYMPUTX to create the new macro variable.

WarrenKuhfeld
Ammonite | Level 13

@Kurt_Bremser just gave you the best advice that anyone can give about the macro language. Don't use it when there is a simpler alternative such as DATA step processing.

yabwon
Amethyst | Level 16

Use the basePlus package's %zipEvalf() macro:

 

%loadPackage(BasePlus)

resetline;
%let My1=X Y Z;

%let My2 =%zipEvalf(&My1., %str(%'), function=quote);

%put &=my1. &=my2.;

yabwon_0-1765102160881.png

 

 

 

Bart

_______________
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



Patrick
Opal | Level 21

Here one way to go

%let My1=X Y Z;
%let My2=%sysfunc(prxchange(s/(\w+)/'$1'/i,-1,&My1));
%put &=My2;
 69         %let My1=X Y Z;
 70         %let My2=%sysfunc(prxchange(s/(\w+)/'$1'/i,-1,&My1));
 71         %put &=My2;
 MY2='X' 'Y' 'Z'
Quentin
Super User

Take a look at Richard DeVenezia's  %seplist() utility.  It will do that and more.  https://www.devenezia.com/downloads/sas/macros/index.php?m=seplist 

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

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
  • 400 views
  • 5 likes
  • 7 in conversation