DATA Step, Macro, Functions and more

Count composite character strings, in a Macro List, that contain special character i.e. "_", "."

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

Count composite character strings, in a Macro List, that contain special character i.e. "_", "."

Dear all, I have created a macro list (using Proc Sql). The members of this macro list are "composite" character string i.e. WORK.DSN1 WORK.DSN2 WORK.DSN3 WORK.DSN4 etc. I tried to count them by using COUNTW but the result doubles the actual number ... for each "composite" string WORK.DSN1 counts both WORK and DSN1 as separate words...I would like to my composite strings to be counted once and not be separated by "." . Any hint or advice would be lore than welcome. Thank you in advance.

Accepted Solutions
Solution
‎06-13-2017 04:14 AM
Trusted Advisor
Posts: 1,137

Re: Count composite character strings, in a Macro List, that contain special character i.e. "_

Posted in reply to Zeus_Olympous

Not sure if you tried to include the delimiter in countw function as below

 


data have;
length string $100.;
string='WORK.DSN1 WORK.DSN2 WORK.DSN3 WORK.DSN4';
c=countw(string,' ');
run;
Thanks,
Jag

View solution in original post


All Replies
Solution
‎06-13-2017 04:14 AM
Trusted Advisor
Posts: 1,137

Re: Count composite character strings, in a Macro List, that contain special character i.e. "_

Posted in reply to Zeus_Olympous

Not sure if you tried to include the delimiter in countw function as below

 


data have;
length string $100.;
string='WORK.DSN1 WORK.DSN2 WORK.DSN3 WORK.DSN4';
c=countw(string,' ');
run;
Thanks,
Jag
Super User
Super User
Posts: 7,039

Re: Count composite character strings, in a Macro List, that contain special character i.e. "_

Posted in reply to Zeus_Olympous

You need to give the COUNTW() function the optional third argument that specifies what charater(s) to consider the delimiter between words.

%put %sysfunc(countw(a.b c.d,%str( )));
PROC Star
Posts: 733

Re: Count composite character strings, in a Macro List, that contain special character i.e. "_

Posted in reply to Zeus_Olympous

use COUNTW function with ' ' as second parameter 

Super User
Super User
Posts: 7,942

Re: Count composite character strings, in a Macro List, that contain special character i.e. "_

Posted in reply to Zeus_Olympous

In the same step as you create the macro list just add:

select count(*) from SASHELP.VTABLES
where LIBNAME="WORK" and memname like "DSN%";

In fact, you could use the data returned from the metadata to directly work with, taking out a layer of abstraction:

E.g.:

data _null_;
  set sashelp.vtable (where=(libname="WORK" and substr(memname,1,3)="DSN"));
  call execute('%do_somthing(indata='||strip(name)||';');
run;

This would generate a % call for each dataset fulfilling the where clause, in your example, for example:

%do_something(indata=dsn1);

%do_something(indata=dsn2);

%do_something(indata=dsn3);

Contributor
Posts: 45

Re: Count composite character strings, in a Macro List, that contain special character i.e. "_

Many thank to all of you for your prompt and helpful response. Unfortunately I cannot give "the solution" remark to all of the answers
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 147 views
  • 4 likes
  • 5 in conversation