- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
That shown style of looping over a list with %DO %UNTIL was common prior to the introduction of the COUNTW function. I think COUNTW was introduced in v7 or v8.
I would agree that using COUNTW to count the number of items in a list seems clearer to me also.
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now at https://www.basug.org/events.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Agree with others here - I would just add that with COUNTW and SCAN (and similar), it's a good idea to specify your delimiter as the optional last argument - otherwise, SAS will try to guess. And further, if the delimiter is a space (as it is here), never a bad idea to clean up potential multiple whitespace characters with %CMPRES:
%let YRLIST=2012 2014 2016;
%let YRLIST=%CMPRES(&YRLIST);
%let nYRS=%sysfunc(countW(&YRLIST,' '));
.... %let YR=%SCAN(&YRLIST, &i, ' ');
* you can also use %STR( ) instead of ' ' above ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks a lot for your comments! I tested your examples and somehow got something like this (see below). I have three questions: 1) it looks like the %cmpres function is not necessary (at least in my example), if I run the %cmpres statement I got error message; 2) if I want to clean up whitespace characters the function to be use is compbl (I do not know how this function is written when it is a macro function), not compress, am I right or wrong? 3) this question is not relevant, how can I make the code posted here colored?
(after I post THIS message I found it is colored somehow, however, the code in my previous post were not)
%let yrlist=%nrstr(2010 % 2012 2014,2016 2018 & 2019);
*%let yrlist=%cmpres(&yrlist,'%,&');
*%put &yrlist;
%let nyrs=%sysfunc(countw(&yrlist,' %,&'));
%put &nyrs;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@quickbluefish wrote:
Agree with others here - I would just add that with COUNTW and SCAN (and similar), it's a good idea to specify your delimiter as the optional last argument - otherwise, SAS will try to guess. And further, if the delimiter is a space (as it is here), never a bad idea to clean up potential multiple whitespace characters with %CMPRES:
%let YRLIST=2012 2014 2016; %let YRLIST=%CMPRES(&YRLIST); %let nYRS=%sysfunc(countW(&YRLIST,' ')); .... %let YR=%SCAN(&YRLIST, &i, ' '); * you can also use %STR( ) instead of ' ' above ;
I agree it's good practice to specify the delimiter for COUNTW and %SCAN. Particularly because if you have an empty list, %sysfunc(countw()) will throw an error, but %sysfunc(countw(,%str( ))) will return 0.
That said, I think it's much clearer/better to use %STR( ) to specify the blank as delimiter, rather than ' '. If you use ' ' you're telling the macro processor to use both blank and a single quote as delimiters. In the macro language ' ' is a three character string, not a single blank.
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now at https://www.basug.org/events.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- « Previous
- Next »