SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

How to count frequency of columns?

Reply
Frequent Contributor
Posts: 75

How to count frequency of columns?

The data looks like this:

data temp;

input ID Var1 Var2 Var3;

datalines;

001 x11 x12 x13

002 x21 .     x23

003 .      .    .  ;

I want to create a new variable (Count) which counts the frequency of valid values from Var1 to Var3, so the new data looks like:

ID   Var1 Var2  Var3  Count

001 x11   x12   x13    3

002 x21   .       x23    2

003 .       .        .       .

;

I'm not sure if the count value for ID=003 is missing value or 0, but the idea is basically the same.

Trusted Advisor
Posts: 1,228

Re: How to count frequency of columns?

Posted in reply to NonSleeper

data want;

set temp;

count=0;

array v var:;

do over v;

if v ne ' ' then count+1;

end;

run;

Super Contributor
Posts: 298

Re: How to count frequency of columns?

Posted in reply to NonSleeper

Will this help you?

data want;

   set temp;

   array k

  • var1 - var3;
  •    count = dim(k) - cmiss(of k

  • );
  • run;

    Super User
    Posts: 19,770

    Re: How to count frequency of columns?

    Use the n() function, if the data is numeric not character.

    data want;

       set temp;

       array k

  • var1 - var3;
  •    count = n(of k

  • );
  • run;

    Ask a Question
    Discussion stats
    • 3 replies
    • 300 views
    • 0 likes
    • 4 in conversation