DATA Step, Macro, Functions and more

Do loop or array?

Reply
Contributor QLi
Contributor
Posts: 59

Do loop or array?

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.
Frequent Contributor
Posts: 139

Re: Do loop or array?

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;
Super Contributor
Super Contributor
Posts: 365

Re: Do loop or array?

Posted in reply to darrylovia
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
Occasional Contributor
Posts: 14

Re: Do loop or array?

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;
Super User
Posts: 10,041

Re: Do loop or array?

[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
Valued Guide
Posts: 2,177

Re: Do loop or array?

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Do loop or array?

Although I believe that the COUNT function relates only to SAS CHARACTER type variables.

Scott Barry
SBBWorks, Inc.
Respected Advisor
Posts: 3,799

Re: Do loop or array?

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]
  • Regular Contributor
    Posts: 241

    Re: Do loop or array?

    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;

    Valued Guide
    Posts: 2,177

    Re: Do loop or array?

    Posted in reply to chang_y_chung_hotmail_com
    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/'
    Ask a Question
    Discussion stats
    • 9 replies
    • 344 views
    • 0 likes
    • 9 in conversation