BookmarkSubscribeRSS Feed
NonSleeper
Quartz | Level 8

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.

3 REPLIES 3
stat_sas
Ammonite | Level 13

data want;

set temp;

count=0;

array v var:;

do over v;

if v ne ' ' then count+1;

end;

run;

KachiM
Rhodochrosite | Level 12

Will this help you?

data want;

   set temp;

   array k

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

  • );
  • run;

    Reeza
    Super User

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

    data want;

       set temp;

       array k

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

  • );
  • run;

    sas-innovate-white.png

    Special offer for SAS Communities members

    Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

     

    View the full agenda.

    Register now!

    How to connect to databases in SAS Viya

    Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

    Find more tutorials on the SAS Users YouTube channel.

    Discussion stats
    • 3 replies
    • 1293 views
    • 0 likes
    • 4 in conversation