## Text equivalent of N function

Solved
Frequent Contributor
Posts: 93

# Text equivalent of N function

`NO_OF_PRINCIPALS = N(NRIC1,NRIC2,NRIC3,NRIC4,NRIC5,NRIC6,NRIC7);`

N only works for numeric columns. Is there any equivalent for text fields?

Accepted Solutions
Solution
‎03-18-2012 01:02 PM
Posts: 5,523

## Text equivalent of N function

`NO_OF_PRINCIPALS = 7 - cmiss(NRIC1,NRIC2,NRIC3,NRIC4,NRIC5,NRIC6,NRIC7);`

PG

PG

All Replies
PROC Star
Posts: 8,163

## Text equivalent of N function

I'm not aware of a function that can do it, but you can always do it using proc freq and/or proc freq with a format. e.g., take a look at http://www.ats.ucla.edu/stat/sas/faq/cnt_charmiss.htm

Solution
‎03-18-2012 01:02 PM
Posts: 5,523

## Text equivalent of N function

`NO_OF_PRINCIPALS = 7 - cmiss(NRIC1,NRIC2,NRIC3,NRIC4,NRIC5,NRIC6,NRIC7);`

PG

PG
Frequent Contributor
Posts: 93

## Text equivalent of N function

NO_OF_PRINCIPALS = 7 - cmiss(NRIC1,NRIC2,NRIC3,NRIC4,NRIC5,NRIC6,NRIC7);

This is exactly what I wanted, and I can see why it will work, but unfortunately my company is still using 9.1.3. I prefer one-line functions.

Meanwhile I am using the verbose IF THEN Clause:

IF NRIC1='' THEN NO_OF_PRINCIPALS = 0;

ELSE IF NRIC2='' THEN NO_OF_PRINCIPALS = 1;

ELSE IF NRIC3='' THEN NO_OF_PRINCIPALS = 2;

ELSE IF NRIC4='' THEN NO_OF_PRINCIPALS = 3;

ELSE IF NRIC5='' THEN NO_OF_PRINCIPALS = 4;

ELSE IF NRIC6='' THEN NO_OF_PRINCIPALS = 5;

ELSE IF NRIC7='' THEN NO_OF_PRINCIPALS = 6;

ELSE NO_OF_PRINCIPALS = 7;

Posts: 1,318

## Text equivalent of N function

This is definitly not an optimal solution however it will work in 9.1.3, as long as you have at least SP4 applied (when proc fcmp was added) and it provides the desired one-line functionality to you datastep...

proc fcmp outlib=work.func.char;

function c(arr

• \$) varargs;
•   cnt=dim(arr);

do i=1 to dim(arr);

cnt=cnt-missing(arr);

end;

return(cnt);

endsub;

run;

options cmplib=work.func;

data _null_;

array chars[5] \$ _temporary_ (3*'a' 2*' ');

c=c(chars);

put c;

run;

Super User
Posts: 10,766

## Re: Text equivalent of N function

ARRAY is a good friend.

```data x;
input  (NRIC1-NRIC7) (\$);
cards;
a b d 3 . e .
w d f g e t 3
c . k l . . l
l l . . . p .
;
run;
data want(drop=i);
set x;
array _x{*} \$ NRIC1-NRIC7;
nmiss=0;
do i=1 to dim(_x);
nmiss+missing(_x{i});
end;
NO_OF_PRINCIPALS=dim(_x)-nmiss;
run;
```

Ksharp

Posts: 3,167

## Re: Text equivalent of N function

I like this one, avoiding hard-coding is better.

🔒 This topic is solved and locked.

Discussion stats
• 6 replies
• 234 views
• 1 like
• 6 in conversation