Help using Base SAS procedures

Either field condition

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Either field condition

Hi,

I need help coding a condition. I have four numeric fields and I want to say if any one is zero then do a certain action. Data looks like this:

A     B     C     D

1     0     0     2

3     1     2     0

4     2     8     4

0     6     1     9

I want to say if these fields contain one zero then do this, two zeros then do that action. I know we can do it by making all possibilities one after another like If A = 0 and b ne 0 and c ne 0 and d ne 0 but wanted to know whether there is any other way to avoid all that code.

Thanks in advance for the help.

-Akber.


Accepted Solutions
Solution
‎10-30-2012 10:44 PM
Super Contributor
Posts: 644

Re: Either field condition

I had much the same idea but using the sum function.  Your 1* are redundant and will add to the processing time.

Data want ;

    Set     have ;

    X     = Sum ((A = 0), (B = 0), (C = 0), (D = 0)) ;

    Select (X) ;

        When (1)

          Do ;

            /* Action when 1 zero */

          End ;

        When (2)

          Do ;

            /* Action when 2 zeros */

          End ;

        When (3)

          Do ;

            /* Action when 3 zeros */

          End ;

        Otherwise

          Do ;

            /* Action when no zeros */

          End ;

      End ;

Run ;

A B C D X

1 0 0 2 2

3 1 2 0 1

4 2 8 4 0

0 6 1 9 1

Richard in Oz

View solution in original post


All Replies
Valued Guide
Posts: 634

Re: Either field condition

Posted in reply to akberali67

The variable NUM in the following step will contain the number of variables with a value of 0.

data want;

input A     B     C     D;

num = 1*(a=0)

     +1*(b=0)

     +1*(c=0)

     +1*(d=0);

put num=;

datalines;

1     0     0     2

3     1     2     0

4     2     8     4

0     6     1     9

run;

Solution
‎10-30-2012 10:44 PM
Super Contributor
Posts: 644

Re: Either field condition

I had much the same idea but using the sum function.  Your 1* are redundant and will add to the processing time.

Data want ;

    Set     have ;

    X     = Sum ((A = 0), (B = 0), (C = 0), (D = 0)) ;

    Select (X) ;

        When (1)

          Do ;

            /* Action when 1 zero */

          End ;

        When (2)

          Do ;

            /* Action when 2 zeros */

          End ;

        When (3)

          Do ;

            /* Action when 3 zeros */

          End ;

        Otherwise

          Do ;

            /* Action when no zeros */

          End ;

      End ;

Run ;

A B C D X

1 0 0 2 2

3 1 2 0 1

4 2 8 4 0

0 6 1 9 1

Richard in Oz

PROC Star
Posts: 7,492

Re: Either field condition

Posted in reply to RichardinOz

Or, a third approach:

data have;

  input A     B     C     D;

  cards;

1     0     0     2

3     1     2     0

4     2     8     4

0     6     1     9

;

data want (drop=_Smiley Happy;

  set have;

  set have (rename=(A=_A b=_b c=_c d=_d));

  array _x _a--_d;

  do over _x;

    _x=_x=0;

  end;

  number_of_zeros=sum(of _x(*));

run;

Respected Advisor
Posts: 3,156

Re: Either field condition

A slightly briefier third approach:

data want;

  set have;

  array x _numeric_;

  number_of_zeros=0;

  do over x;

  number_of_zeros+x=0;

  end;

run;

Haikuo

PROC Star
Posts: 7,492

Re: Either field condition

Haikuo,

Let's add one more reduction  .. remove the need to initialize number_of_zeros:

data want;

  set have;

  array x _numeric_;

  do over x;

    number_of_zeros=sum(number_of_zeros,x=0);

  end;

run;

Valued Guide
Posts: 634

Re: Either field condition

Posted in reply to RichardinOz

@RichardinOz  I like the use of the SUM function over my statement.  Much cleaner code.

Super Contributor
Posts: 644

Re: Either field condition

Thanks Art.  It's a case of 'spot the obvious mistake' because I left out the condition

         When (4)

          Do ;

            /* Action when 4 zeros */

          End ;

BTW the 1* construct is a hangover from very old coding practices and superseded by the use of, for example, LENGTH statements to specify numeric variables, or the INPUT() function to manage type conversions.

Richard in Oz

Respected Advisor
Posts: 3,156

Re: Either field condition

Posted in reply to akberali67

The more, the merrier, hence the fourth approach:

data have;

  input A B C D;

  cards;

1 0 0 2

3 1 2 0

4 2 8 4

0 6 1 9

;

data want;

  set have;

  numberof0=lengthn(cats(of _all_))-lengthn(compress(cats(of _all_),'0'));

run;

proc print;run;

Haikuo

PROC Star
Posts: 7,492

Re: Either field condition

: One problem with that method is that it will treat ALL zeros as relevant.  e.g.:

data have;

  input A B C D;

  cards;

10 0 0 2

3 1 2 0

4 2 8 4

0 6 1 9

;

data want;

  set have;

  numberof0=lengthn(cats(of _all_))-lengthn(compress(cats(of _all_),'0'));

run;

Super User
Posts: 5,518

Re: Either field condition

Here's a more obscure exception.

options missing='0';

data test;

   a=1;

   b=2;

   c=.;

   d=4;

   combined = cats(of _all_);

   put combined=;

run;

Because of the OPTION setting, missing values get converted to "0" in the combined string.

Respected Advisor
Posts: 3,156

Re: Either field condition

Oops. True. Crying for morning coffee! Haikuo

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 305 views
  • 9 likes
  • 6 in conversation