BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hellind
Quartz | Level 8

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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

How about :

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

PG

PG

View solution in original post

6 REPLIES 6
art297
Opal | Level 21

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

PGStats
Opal | Level 21

How about :

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

PG

PG
hellind
Quartz | Level 8

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;

FriedEgg
SAS Employee

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;

    Ksharp
    Super User

    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

    Haikuo
    Onyx | Level 15

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

    SAS Innovate 2025: Save the Date

     SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

    Save the date!

    How to Concatenate Values

    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.

    SAS Training: Just a Click Away

     Ready to level-up your skills? Choose your own adventure.

    Browse our catalog!

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