turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Count numeric values in macro parameter

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-27-2016 02:54 PM

I am trying to calculate the number of numeric values in a macro parameter.

The code below works for character strings-

%let x = A B C;

%let n = %SYSFUNC(COUNTW(&x));

The code below does not work for numeric values-

%let x = 10 20 50;

%let n = %SYSFUNC(COUNT(&x));

I know COUNT does not work like this. Which function i can use to solve it?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ujjawal

04-27-2016 03:01 PM

For your example, you can still use COUNTW.

Do you have more complex situations, such as:

%let x = 20 abc 50 A1;

If so, what would you like the result to be?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

04-27-2016 03:32 PM - edited 04-27-2016 03:35 PM

%let x = 20 abc 50 A1;

It should return 2 since two values are numeric.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ujjawal

04-27-2016 03:36 PM

Counting numeric vs. character separately is much harder.

But COUNTW will work with a list of numeric values. There must be some other problem. You can always post the code that gives you a blank.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ujjawal

04-27-2016 09:49 PM

How about this :

```
%macro count_digit(t=);
%local temp count;
%let count=0;
%do i=1 %to %sysfunc(countw(&t));
%let temp=%scan(&t,&i);
%if %datatyp(&temp)=NUMERIC %then %do;
%let count=%eval(&count+1);
%end;
%end;
&count
%mend;
%let x = 20 abc 50 A1;
%let n = %count_digit(t=&x);
%put &n;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

04-28-2016 09:48 AM

Great suggestion from @Ksharp (as always). The %DATATYP autocall macro would even recognize leading plus or minus signs, decimal points or scientific notation (...E-6) as valid parts of numeric values. If this more general type of numeric "words" could occur, however, the sets of delimiters used by both COUNTW and %SCAN should be restricted (e.g. reduced to blanks, as shown below), as otherwise plus and minus signs and decimal points would be regarded as delimiters:

countw(&t,%str( ))

%scan(&t,&i,%str( ))

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ujjawal

04-27-2016 05:14 PM

Probably misunderstanding how COUNT works. Count counts the number of times a substring appears in a string.

Count("xyxzxb 123","x") would return 3 times that X appears in the string.

%let x= 1,2,3;

%let count=%sysfunc(n(x)); would give you count but the list would have to be comma separated which in general is a suboptimal choice with macro variables and fails miserably if any nonnumeric noncomma characters are encountered.