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

I have following macro variable :

 

 

%let NAME= NAME=Carlotta's Kitchen LLC,Chan & Chan (USA) Inc.,Cle-Mor Market,Clint & Sons Beef Jerky,D&D Foods Inc.,D.I.M. Inc;

 

How can create a Name_List macro from Name macro as shown below.

 

Name_List = 'Carlotta's Kitchen LLC','Chan & Chan (USA) Inc.','Cle-Mor Market,Clint & Sons Beef Jerky','D&D Foods Inc.','D.I.M. Inc';

 

I tried as 

 

%global NAME Name_List;

 

%Macro Name_LIST;
%let Name_LIST =;

%let count=%sysfunc(countw(%bquote(&NAME),%str(,)));
%put &count;
%do i=1 %to &count;
%let Name_LIST =%qsysfunc(tranwrd(&Name_LIST %str(%')%scan(%bquote(&NAME),&i,%str(,))%str(%'),
%str( ),%str(, )));
%end;

%Mend;

%Name_LIST
%let Name_LIST = %superq(Name_LIST);  
 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Don't use macro code to work with messy strings.  Use SAS code instead.

478  data _null_;
479    call symputx('name'
480  ,'NAME=Carlotta''s Kitchen LLC,Chan & Chan (USA) Inc.,Cle-Mor Market,Clint & Sons Beef Jerky,D&D Foods Inc.,D.I.M. Inc'
481  );
482
483  run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


484
485  %put %superq(name);
NAME=Carlotta's Kitchen LLC,Chan & Chan (USA) Inc.,Cle-Mor Market,Clint & Sons Beef Jerky,D&D Foods Inc.,D.I.M. Inc
486
487  data _null_;
488    length old new $32767;
489    old=symget('name');
490    old=left(substr(old,index(old,'=')+1));
491    do i=1 to countw(old,',');
492      new=catx(',',new,quote(strip(scan(old,i,',')),"'"));
493    end;
494    call symputx('name_list',new);
495  run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


496
497  %put &name_list;
'Carlotta''s Kitchen LLC','Chan & Chan (USA) Inc.','Cle-Mor Market','Clint & Sons Beef Jerky','D&D Foods Inc.','D.I.M. Inc'

View solution in original post

6 REPLIES 6
yabwon
Onyx | Level 15

Do it with datastep:

1) symget() to store macrovaraible in a variable

2) do-loop with scan(,i,",") witch comma as separator

3) use quote(,) function to add quotes,

4) combine it in 1 string with catx(",",....)

and

5) call symputX() to push to macroworld again.

_______________
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



Quentin
Super User

I'm a fan of Richard DeVenezia's %seplist() macro for adding delimiters, quotes, etc to lists of items.

 

https://www.devenezia.com/downloads/sas/macros/index.php?m=seplist

 

What you've got is a nasty macro quoting puzzle, which usually can be avoided, but as long as you quote the value, %seplist willl keep it quoted.

 

This code:

data _null_ ;
  call symputx("namelist",'Carlotta''s Kitchen LLC,Chan & Chan (USA) Inc.,Cle-Mor Market,Clint & Sons Beef Jerky,D&D Foods Inc.,D.I.M. Inc') ;
run ;
%put namelist=%superq(namelist) ;

%let namelist2= %seplist(%superq(namelist),nest=Q,indlm=%str(,)) ;
%put namelist2=&namelist2 ;

 Returns:

1    data _null_ ;
2      call symputx("namelist",'Carlotta''s Kitchen LLC,Chan & Chan (USA) Inc.,Cle-Mor Market,Clint &
2  ! Sons Beef Jerky,D&D Foods Inc.,D.I.M. Inc') ;
3    run ;


4    %put namelist=%superq(namelist) ;
namelist=Carlotta's Kitchen LLC,Chan & Chan (USA) Inc.,Cle-Mor Market,Clint & Sons Beef Jerky,D&D
Foods Inc.,D.I.M. Inc
5
6    %let namelist2= %seplist(%superq(namelist),nest=Q,indlm=%str(,)) ;
7    %put namelist2=&namelist2 ;
namelist2='Carlotta's Kitchen LLC','Chan & Chan (USA) Inc.','Cle-Mor Market','Clint & Sons Beef
Jerky','D&D Foods Inc.','D.I.M. Inc'

It doesn't remove the macro quoting, doesn't try to resolve any masked macro tokens, and doesn't get tripped up by the unmatched single quote.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Tom
Super User Tom
Super User

How did you create that macro variable with the unbalanced quotes to begin with?

What are you doing to do with the cleaned up macro variable that looks like that?

Why not just leave the data in a dataset?

GPatel
Pyrite | Level 9

To All:

I have a HTML page with drop down (developed with JSON file)  to select name(s) . Once User selects one or more name from the drop down list, it creates a macro variable.

Then, I use this macro variable to filter / subset the records from the dataset - user choice of names - for reporting activities.

 

Regards, GPATEL

Tom
Super User Tom
Super User

You should probably clean up the HTML definitions for the selection list so that it generates a string that does not have unbalanced quotes or unquoted macro triggers like & or %.

 

Tom
Super User Tom
Super User

Don't use macro code to work with messy strings.  Use SAS code instead.

478  data _null_;
479    call symputx('name'
480  ,'NAME=Carlotta''s Kitchen LLC,Chan & Chan (USA) Inc.,Cle-Mor Market,Clint & Sons Beef Jerky,D&D Foods Inc.,D.I.M. Inc'
481  );
482
483  run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


484
485  %put %superq(name);
NAME=Carlotta's Kitchen LLC,Chan & Chan (USA) Inc.,Cle-Mor Market,Clint & Sons Beef Jerky,D&D Foods Inc.,D.I.M. Inc
486
487  data _null_;
488    length old new $32767;
489    old=symget('name');
490    old=left(substr(old,index(old,'=')+1));
491    do i=1 to countw(old,',');
492      new=catx(',',new,quote(strip(scan(old,i,',')),"'"));
493    end;
494    call symputx('name_list',new);
495  run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


496
497  %put &name_list;
'Carlotta''s Kitchen LLC','Chan & Chan (USA) Inc.','Cle-Mor Market','Clint & Sons Beef Jerky','D&D Foods Inc.','D.I.M. Inc'

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 906 views
  • 2 likes
  • 4 in conversation