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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 523 views
  • 2 likes
  • 4 in conversation