BookmarkSubscribeRSS Feed

All the major SAS string handling functions have a macro equivalent:

%index

%substr

%scan

%upcase

%length

 

Especially when using %scan, one often has to use %sysfunc(countw()) to initialize a macro loop.

 

So I propose to create a macro equivalent of countw, namely %countw, similar in function to %scan with regards to delimiters and modifiers.

6 Comments
Quentin
Super User

I don't often react to proposals with "but you can already do it like ... ", but in this case I'm wondering why having %countw would be better than %sysfunc(countw())?  (Other than the fact that I mistype %sysfunc about 30% of the time).

 

I suppose it would highlight it in the documentation, when someone was browsing for macro functions. Are you thinking there might be an efficiency benefit to having a native macro function?

 

I haven't been around long enough to know the history.  I assume %sysfunc() was added to the macro language at some point.  Could it be that the functions you listed were around before there was %sysfunc, and now that they gave us %sysfunc, there isn't a need for SAS to develop more macro functions that are translations of data step functions?

Kurt_Bremser
Super User

Key sentence:

(Other than the fact that I mistype %sysfunc about 30% of the time)

The proposed function would reduce complexity in typing and reading code. Given the fact that the macro facility is a pure text processor, having native string handling functions only seems natural. Especially when one thinks that %scan() is already there, and I rarely use scan() in a datastep without countw(), and %scan() without %sysfunc(countw()).

barefootguru
Fluorite | Level 6

It's a bit of a slippery slope… there's a lot of data step functions that could be added to the macro language.

 

@Quentin is correct:  the existing macro string handling was present before the addition of %sysfunc, and macro and data step were at least on the same playing field for capability.  Then %sysfunc was added and there's been almost no new macro functions added since.

 

It is unwieldy, particularly when you embed %sysfunc within %sysfunc, but the power's there Cat Wink

ChrisNZ
Tourmaline | Level 20

I find countw() is a large proportion of the cases for which I use %sysfunc(). So yes, obviously needed.

ChrisNZ
Tourmaline | Level 20

>  there's been almost no new macro functions added since.

True, but maybe not as relevant as you think. The macro functions keep evolving. For example, %scan() used to accept only 2 parameters. Then it was aligned with the improved scan() function when more parameters were added to scan(). So it's not like this area is dormant, as you seem to imply.

Peter_L
Quartz | Level 8

%scan returns an empty string if the index is greater than the last word. I just test for an empty string to end the loop. What I would really like here is a structured way to break out of a loop, but that's another story.