creating a counting variable

  • Follow


I have a set of variables that I need to create a new variable from by
counting the number of 1s (as opposed to 2s). The new variable would
give the total number of 1s for all 17 of the old variables and
therefore would be set to 0 if all of the old variables were 2s. I am
new to SAS, so you may have to talk me through this a bit.

Thanks,
Anne
0
Reply amz51076 (1) 12/24/2009 12:16:58 AM

On Dec 23, 7:16=A0pm, Anne <amz51...@gmail.com> wrote:
> I have a set of variables that I need to create a new variable from by
> counting the number of 1s (as opposed to 2s). The new variable would
> give the total number of 1s for all 17 of the old variables and
> therefore would be set to 0 if all of the old variables were 2s. I am
> new to SAS, so you may have to talk me through this a bit.

A set of variables can be organized as an array for processing
purposes.
You would then loop over the array and perform your criteria tests on
each element in the array.
-------
data foo;
  do rowid =3D 1 to 10;
    array x(17);
    do _n_ =3D 1 to dim(x);
      x(_n_) =3D floor (4*ranuni(1234));
    end;
    output;
  end;
run;

data foo_with_count;
  set foo;
  array x(17);
  one_count =3D 0;
  do _n_ =3D 1 to dim(x);
    one_count + (x[_n_] =3D 1);
  end;
  format _numeric_ 3.;
run;
-------

There are numerous other ways to specify which variables are to be in
a DATA Step array.  How you do it depends on the actual variable names
that you are dealing with.

Richard A. DeVenezia
http://www.devenezia.com
0
Reply Richard 12/24/2009 12:51:53 AM


Just another way how to do it:

data foo;
  do rowid = 1 to 10;
    array x(17);
    do _n_ = 1 to dim(x);
      x(_n_) = floor (4*ranuni(1234));
    end;
    output;
  end;
run;

data want;
  set foo;
  count1=length(cats(of x:))-length(compress(cats(of x:),'1'));
  put count1= ;
run;

0
Reply Patrick 12/24/2009 5:57:03 AM

On Dec 23, 7:51=A0pm, "Richard A. DeVenezia" <rdevene...@gmail.com>
wrote:
> On Dec 23, 7:16=A0pm, Anne <amz51...@gmail.com> wrote:
>
> > I have a set of variables that I need to create a new variable from by
> > counting the number of 1s (as opposed to 2s). The new variable would
> > give the total number of 1s for all 17 of the old variables and
> > therefore would be set to 0 if all of the old variables were 2s. I am
> > new to SAS, so you may have to talk me through this a bit.
>
> A set of variables can be organized as an array for processing
> purposes.
> You would then loop over the array and perform your criteria tests on
> each element in the array.
> -------
> data foo;
> =A0 do rowid =3D 1 to 10;
> =A0 =A0 array x(17);
> =A0 =A0 do _n_ =3D 1 to dim(x);
> =A0 =A0 =A0 x(_n_) =3D floor (4*ranuni(1234));
> =A0 =A0 end;
> =A0 =A0 output;
> =A0 end;
> run;
>
> data foo_with_count;
> =A0 set foo;
> =A0 array x(17);
> =A0 one_count =3D 0;
> =A0 do _n_ =3D 1 to dim(x);
> =A0 =A0 one_count + (x[_n_] =3D 1);
> =A0 end;
> =A0 format _numeric_ 3.;
> run;
> -------
>
> There are numerous other ways to specify which variables are to be in
> a DATA Step array. =A0How you do it depends on the actual variable names
> that you are dealing with.
>
> Richard A. DeVeneziahttp://www.devenezia.com

Thank you for the reply. However, that's almost completely greek to
me. For reference, I'm about at the Little SAS Book level of
proficiency. Can you perhaps break it down a little further?

Anne
0
Reply Anne 12/25/2009 12:35:50 AM

Anne

You will learn most by looking up all language elements in the doku
http://support.sas.com/onlinedoc/913/docMainpage.jsp (BAse SAS/SAS
Language Reference: Dictionary).


Here some explanations:


The data foo step does nothing else than creating 10 example
observations.

data foo;

  /* 10 iterations, rowid will have values 1,2,3...10 */
  do rowid = 1 to 10;

    /* creates an array 'x' with 17 elements x1 - x17   */
    array x(17);

    /* loops over every single array element, starting with x1 and
ending with x17. The dim() function retrieves the number of array
elements <-  array x(17); ) */
    do _n_ = 1 to dim(x);

      /* That's a very common way of how to create a set of random
numbers within a defined number range. Look up the functions and try
to understand how it works */
      x(_n_) = floor (4*ranuni(1234));
    end;

    /* write to output data set "foo". This is inside the outer do
loop so 10 observations will be written after x1 - x17 are populated
for this observation */
    output;
  end;
run;



data want;
  set foo;

  /* The functions here are nested. Resolving the solutions happens
from inside to outside the brakets. The basic idea
is:                      */
  /* Create a string which contains all values vars x1 - x17. This
string should have a length of 17 (17*1 digit from x1 -
x17)                 */
  /* Use compress() function compressing all 1's. Using length() on
the resulting string tells us how many 1's have been in the source
string.  */
  count1=length(cats(of x:))-length(compress(cats(of x:),'1'));
  put count1= ;
run;



length(cats(of x:))-length(compress(cats(of x:),'1'));


'of x:' addresses all variables where the name starts with 'x'

cats(of x:): cats() concatenates numeric and/or character variables
and also removes all leading and trailing blanks

length(cats(of x:)): length() gives the position of the last non-blank
value in a string.


compress(cats(of x:),'1'): removes all 1's from the resulting string
of cats() function, i.e. compress('212112','1') results in string
'222'  */


count1=length(cats(of x:))-length(compress(cats(of x:),'1')); The
difference in length between the compressed string and the non-
compressed string is equal to the number of 1' in the original string


HTH
Patrick
0
Reply Patrick 12/25/2009 1:42:09 AM

4 Replies
530 Views

(page loaded in 0.112 seconds)

Similiar Articles:













7/23/2012 11:30:56 AM


Reply: