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);
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'
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.
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.
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?
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
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 %.
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'
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!
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.
Ready to level-up your skills? Choose your own adventure.