DATA Step, Macro, Functions and more

Text equivalent of N function

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 90
Accepted Solution

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
Respected Advisor
Posts: 4,654

Text equivalent of N function

How about :

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

PG

PG

View solution in original post


All Replies
PROC Star
Posts: 7,363

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
Respected Advisor
Posts: 4,654

Text equivalent of N function

How about :

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

PG

PG
Frequent Contributor
Posts: 90

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;

Trusted Advisor
Posts: 1,300

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: 9,687

    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

    Respected Advisor
    Posts: 3,124

    Re: Text equivalent of N function

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

    ☑ This topic is SOLVED.

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

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