BookmarkSubscribeRSS Feed
QLi
Fluorite | Level 6 QLi
Fluorite | Level 6
I need count each obs non zero column,

data like this:

data a;
input m1-m12 @@;
cards;
0 1 4 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 5 0 6 0
;
run;

How can I get non zeros fro each obs?


Thanks in advance.
9 REPLIES 9
darrylovia
Quartz | Level 8
Generally with an array you use some type of do loop. See my example below:


data b;
set a;

array m(12);
zero=0;
do i=1 to dim(m);
zero=zero+(m ne 0);
end;
run;
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Darrylovia,

I am sure the summing line of your code should look like:
[pre]
zero=zero+(m[i] ne 0);
[/pre]
This is a problem of this forum software. Please, see http://support.sas.com/forums/thread.jspa?messageID=27609

Sincerely,
SPR
Ankitsas
Calcite | Level 5
Friend,

You can use the below code to count the number of zeros in an observation

data a;
input m1-m12;
cards;
0 1 4 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 5 0 6 0
;
run;
data aa;
set a;
count = 0;
array a[12] m1-m12;
do i = 1 to 12;
if a = 0 then count+1;
end;
run;
Ksharp
Super User
[pre]
data a;
input m1-m12;
cards;
0 1 4 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 5 0 6 0
;
run;
data aa;
set a;
count=0;
array a{*} m1-m12;
do i = 1 to dim(a);
if a{i} then count+1;
end;
drop i;
run;
[/pre]


Ksharp Message was edited by: Ksharp
Peter_C
Rhodochrosite | Level 12
does this look like an opportunity for the COUNT() function?
First count all the columns in the macro environment - so counting all cols only once. On each row count occurrence of zeros to subtract from the all-cols count, leaving the non-zero count
without any arrays
Peter
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Although I believe that the COUNT function relates only to SAS CHARACTER type variables.

Scott Barry
SBBWorks, Inc.
data_null__
Jade | Level 19
This is what I came up with using COUNT. It could be made simpler if single digits were assumed. It could also be done without the array using (of m1-m12) in the functions. Six of one, half dozen of another, arrays are just fancy variables lists.

[pre]
data test;
array m[12];
input m
  • ;
    z = cats(';',catx(',;',of m
  • ),',');
    Not0 = n(of m
  • )-count(z,';0,');
    cards;
    0 10 4 0 0 0 0 0 0 0 0 0
    1 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 5 0 6 0
    ;;;;
    run;
    proc print;
    run;
    [/pre]
  • chang_y_chung_hotmail_com
    Obsidian | Level 7

    Here is one way that does not require neither do loops or arrays.

       data one;
          input m1-m12;
       cards;
       0 1 4 0 0 0 0 0 0 0 0 0
       1 0 0 0 0 0 0 0 0 0 0 0
       0 0 0 0 0 0 0 0 5 0 6 0
       ;
       run;

       data two;
          set one;
          zero = sum(m1=0,m2=0,m3=0,m4=0,m5=0,m6=0,
            m7=0,m8=0,m9=0,m10=0,m11=0,m12=0);
          nonzero = 12 - zero;
          put nonzero=;
       run;

    Peter_C
    Rhodochrosite | Level 12
    the idea of count() seems to be a bit of a failure, since the list of columns could as easily be adapted to fill the chang_y_chung solution, as any other.
    for what it is worth, I was seeking

    given a macro variable &names holding the list of columns to be tested for 0

    %let list_count = %sysfunc( countw( &names )) ;
    non_zeroes = &list_count - count( '/'!! catx( '//', of &names ) !!'/', '/0/' );

    using catx() to convert the numerics in the list to character
    then counting the occurrence of '/0/'

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    Register now!

    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.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

    Discussion stats
    • 9 replies
    • 1415 views
    • 0 likes
    • 9 in conversation