BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

Assume I've the macro variable as follows.

 

%let rep_run= closing run insurance,insurance,secondary closing run insurance,business insurance,RI_BCT;

Now I want to create one new macro variable (e.g. rep_run_values ) by using the  macro variable rep_run and I want the value to be enclosed with single quote as mentioned below.

 

'closing run insurance','insurance','secondary closing run insurance','business insurance','RI_BCT'

Any help?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User
%let rep_run= closing run insurance,insurance,secondary closing run insurance,business insurance,RI_BCT;
%let new=%unquote(%str(%')%qsysfunc(tranwrd(%superq(rep_run),%str(,),','))%str(%'));
%put &=new;
405   %let rep_run= closing run insurance,insurance,secondary closing run insurance,business insurance,RI_BCT;
406   %let new=%unquote(%str(%')%qsysfunc(tranwrd(%superq(rep_run),%str(,),','))%str(%'));
407   %put &=new;
NEW='closing run insurance','insurance','secondary closing run insurance','business insurance','RI_BCT'

Convert the commas to ',' and add quotes before and after.  Once the string is quoted there is no longer any need for macro quoting.

View solution in original post

12 REPLIES 12
Tom
Super User Tom
Super User
%let rep_run= closing run insurance,insurance,secondary closing run insurance,business insurance,RI_BCT;
%let new=%unquote(%str(%')%qsysfunc(tranwrd(%superq(rep_run),%str(,),','))%str(%'));
%put &=new;
405   %let rep_run= closing run insurance,insurance,secondary closing run insurance,business insurance,RI_BCT;
406   %let new=%unquote(%str(%')%qsysfunc(tranwrd(%superq(rep_run),%str(,),','))%str(%'));
407   %put &=new;
NEW='closing run insurance','insurance','secondary closing run insurance','business insurance','RI_BCT'

Convert the commas to ',' and add quotes before and after.  Once the string is quoted there is no longer any need for macro quoting.

David_Billa
Rhodochrosite | Level 12

@Tom @PaigeMiller Great! I do have one more question. How to convert the strings to upper case after enclosing it with single quote.

 

Desired result is,

 

'CLOSING RUN INSURANCE','INSURANCE','SECONDARY CLOSING RUN INSURANCE','BUSINESS INSURANCE','RI_BCT'
PaigeMiller
Diamond | Level 26
%let new = %upcase(&new);
--
Paige Miller
David_Billa
Rhodochrosite | Level 12

@Tom If I run the code below, I could see a some blanks in the value of macro variable rprun_pi_val. How to remove the blanks?

 

%let rprun_pi=main close run(VFB),secondary close run,main close run(BBB),impact of overreturn on CSP,baseline for inital recognition(VFB),                    Coverage Units new business,main close run mortality,main close run longevity,main close run lapse down,secondary close run mortality,
	secondary close run longevity,secondary close run lapse down;
%let rprun_pi_val=%upcase(%unquote(%str(%')%qsysfunc(tranwrd(%superq(rprun_pi),%str(,),','))%str(%')));
%put &=rprun_pi_val;

Results:

 

26         %let rprun_pi=main close run(VFB),secondary close run,main close run(BBB),impact of overreturn on CSP,baseline for inital
26       !  recognition(VFB),                    Coverage Units new business,main close run mortality,main close run longevity,main
26       ! close run lapse down,secondary close run mortality,
27         	secondary close run longevity,secondary close run lapse down;
28         %let rprun_pi_val=%upcase(%unquote(%str(%')%qsysfunc(tranwrd(%superq(rprun_pi),%str(,),','))%str(%')));
29         %put &=rprun_pi_val;
RPRUN_PI_VAL='MAIN CLOSE RUN(VFB)','SECONDARY CLOSE RUN','MAIN CLOSE RUN(BBB)','IMPACT OF OVERRETURN ON CSP','BASELINE FOR INITAL 
RECOGNITION(VFB)','                    COVERAGE UNITS NEW BUSINESS','MAIN CLOSE RUN MORTALITY','MAIN CLOSE RUN LONGEVITY','MAIN 
CLOSE RUN LAPSE DOWN','SECONDARY CLOSE RUN MORTALITY','  SECONDARY CLOSE RUN LONGEVITY','SECONDARY CLOSE RUN LAPSE DOWN'

 

Desired results:

 

26         %let rprun_pi=main close run(VFB),secondary close run,main close run(BBB),impact of overreturn on CSP,baseline for inital
26       !  recognition(VFB),                    Coverage Units new business,main close run mortality,main close run longevity,main
26       ! close run lapse down,secondary close run mortality,
27         	secondary close run longevity,secondary close run lapse down;
28         %let rprun_pi_val=%upcase(%unquote(%str(%')%qsysfunc(tranwrd(%superq(rprun_pi),%str(,),','))%str(%')));
29         %put &=rprun_pi_val;
RPRUN_PI_VAL='MAIN CLOSE RUN(VFB)','SECONDARY CLOSE RUN','MAIN CLOSE RUN(BBB)','IMPACT OF OVERRETURN ON CSP','BASELINE FOR INITAL RECOGNITION(VFB)','COVERAGE UNITS NEW BUSINESS','MAIN CLOSE RUN MORTALITY','MAIN CLOSE RUN LONGEVITY','MAIN 
CLOSE RUN LAPSE DOWN','SECONDARY CLOSE RUN MORTALITY','SECONDARY CLOSE RUN LONGEVITY','SECONDARY CLOSE RUN LAPSE DOWN'
Ksharp
Super User
%let rep_run= rprun_pi=main close run(VFB),secondary close run,main close run(BBB),impact of overreturn on CSP,baseline for inital recognition(VFB),                    Coverage Units new business,main close run mortality,main close run longevity,main close run lapse down,secondary close run mortality,
	secondary close run longevity,secondary close run lapse down;

%let new=%str(%')%qsysfunc(prxchange(s/\s*%str(,)\s*/%str(',')/,-1,%qupcase(&rep_run)))%str(%');
%put &=new;
Tom
Super User Tom
Super User

The best way is to not build the macro variable with the spaces to begin with. 

%let rprun_pi=main close run(VFB),secondary close run,main close run(BBB);
%let rprun_pi=&rprun_pi,impact of overreturn on CSP,baseline for inital recognition(VFB);
%let rprun_pi=&rprun_pi,Coverage Units new business,main close run mortality,main close run longevity;
%let rprun_pi=&rprun_pi,main close run lapse down,secondary close run mortality;
%let rprun_pi=&rprun_pi,secondary close run longevity,secondary close run lapse down;

How did you build that original macro variable?

Couldn't you build it with the quotes to start with?  For example if the list of values was in a dataset

proc sql noprint;
  select quote(trim(ds_var),"'") into :mvar separated by ',' 
    from have
  ;
quit;

 

If you are stuck with the list with extra spaces.

  • You could adapt the posted REGEX solution to include the spaces around the commas in the string to be replaced.
  • You could use one of the posted methods that scans the original list and builds the new one.

If you assign a macro variable with the result of %SCAN() then the leading and trailing spaces will not become part of the macro variable.

%let rprun_pi=main close run(VFB),secondary close run,main close run(BBB),impact of overreturn on CSP,baseline for inital recognition(VFB),                    Coverage Units new business,main close run mortality,main close run longevity,main close run lapse down,secondary close run mortality,
	secondary close run longevity,secondary close run lapse down;

%macro quoteit(mvar);
%local i sep word;
%do i=1 %to %sysfunc(countw(%superq(&mvar),%str(,)));
  %let word=%scan(%superq(&mvar),&i,%str(,));
  %if %length(&word) %then %do;&sep.%bquote('&word')%let sep=,;%end;
%end;
%mend quoteit;
%let rprun_pi_val=%quoteit(rprun_pi);
%put &=rprun_pi_val;

 

 

yabwon
Onyx | Level 15

Just for fun, data step approach, with upcase.

%let rep_run= closing run insurance,insurance,secondary closing run insurance,business insurance,RI_BCT;
%put &=rep_run.;

data _null_;
  length text str rep_run $ 32767;
  text = symget("rep_run");

    do _N_= 1 to countw(text, ",");
      str = scan(text, _N_, ",");
      rep_run = catx(",", rep_run, quote(upcase(strip(str)),"'"));
    end;

  call symputx("rep_run", rep_run, "G");
run;

%put &=rep_run.;

 

[EDIT:] If the macro variable contains special characters, e.g. % or &, the code works too.

 

All the best

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



Quentin
Super User

See also Richard DeVenezia's %seplist macro function.  It does that and a lot more.  Very handy.

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

The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.
Ksharp
Super User
%let rep_run= closing run insurance,insurance,secondary closing run insurance,business insurance,RI_BCT;
%let new=%str(%')%qsysfunc(prxchange(s/%str(,)/%str(',')/,-1,%bquote(%upcase(&rep_run))))%str(%');
%put &=new;
Tom
Super User Tom
Super User

Note there is a %QUPCASE() function.

Ksharp
Super User
Sure . I just realize OP need UPCASE it ,after %bquote() .

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 12 replies
  • 1483 views
  • 5 likes
  • 6 in conversation