NO_OF_PRINCIPALS = N(NRIC1,NRIC2,NRIC3,NRIC4,NRIC5,NRIC6,NRIC7);
N only works for numeric columns. Is there any equivalent for text fields?
How about :
NO_OF_PRINCIPALS = 7 - cmiss(NRIC1,NRIC2,NRIC3,NRIC4,NRIC5,NRIC6,NRIC7);
PG
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
How about :
NO_OF_PRINCIPALS = 7 - cmiss(NRIC1,NRIC2,NRIC3,NRIC4,NRIC5,NRIC6,NRIC7);
PG
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;
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
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;
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
I like this one, avoiding hard-coding is better.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.