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

Hello,

 

I would like to know whether there is a way to replace duplicate space to single space for the value of macro variable.

 

For example:

%let _invar=A1 A2   A3;

Here you can see there are more than 1 space between A2 and A3. I would like to update _invar or build a new macro variable (_update) to 'A1 A2 A3'. Only 1 space between each word. What should I do.

 

In data step, I could do

COMPBL ("&_invar.")

 however I am not sure how to do the similar process for a macro variable. Could anyone guide me on this? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You can use %SYSFUNC() in macro code to call many SAS functions.  It works for COMPBL().

%let varlist=A    b  C;
%let varlist=%sysfunc(compbl(&varlist));
%put &=varlist;

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

You can use %SYSFUNC() in macro code to call many SAS functions.  It works for COMPBL().

%let varlist=A    b  C;
%let varlist=%sysfunc(compbl(&varlist));
%put &=varlist;
SASJedi
SAS Super FREQ

Consider using the %QCMPRES autocall macro provided with base SAS. 

Check out my Jedi SAS Tricks for SAS Users
Ksharp
Super User

Jedi,
Also %CMPRES autocall macro as well.

1    %let varlist=A    b  C;
2    %let varlist=%cmpres(&varlist);
3    %put &=varlist;
VARLIST=A b C
4    %let varlist=A    b  C;
5    %let varlist=%qcmpres(&varlist);
6    %put &=varlist;
VARLIST=A b C
Tom
Super User Tom
Super User

Note that those are left overs from before the %SYSFUNC() macro function was created.  Not really any reason to use them now.

SASJedi
SAS Super FREQ

@Ksharp - true, but IMHO, %QCMPRES is generally a "safer" option. Because the results are returned pre-quoted, the macro processor will never process the returned text as additional macro code or references. In my decades of macro programming, about 80% of my troubleshooting efforts (correlating to about 80% of my hair loss 😁) have boiled down to under-quoted text being inadvertently resolved and reprocessed by the macro processor. So I always quote results unless I intend to use send the result back to the macro processor for further processing. For example, if I went with the answer provided by @Tom  I would have used %QSYSFUNC instead fo %SYSFUNC.

 

I guess it all depends on how much hair you are willing to sacrifice 😉 

 

Check out my Jedi SAS Tricks for SAS Users
Tom
Super User Tom
Super User

Macro quoting does not only "solve" problems.  It can also "cause" problems.

So use the version %QSYSFUNC() if you want macro quoting added and %SYSFUNC() if you want macro quoting removed.

SASJedi
SAS Super FREQ

In my experience, less than 1% of macro program failures due to quoting issues have been caused by overquoting - so my default behavior is to quote. In situations where I am absolutely positive that processing the text will not produce characters that can be misinterpreted as code, will sometimes use the "non-Q" version of these functions. 

 

This example was obviously simplified by the OP to clarify the question. Not being sure of the ultimate application (and deeply desiring to preserve the OP's remaining hair 😉) I chose to recommend %QCMPRES and I have not changed my mind. His (and your) mileage may vary, of course, and we're all SAS coders  - free to make our own coding decisions. Having options provided is always nice. 

Check out my Jedi SAS Tricks for SAS Users

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 25. 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
  • 7 replies
  • 693 views
  • 5 likes
  • 4 in conversation