COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### creating a counting variable

• Email
• 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

See related articles to this posting

```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

```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

```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

```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

4 Replies
681 Views

Similar Articles

12/7/2013 8:35:49 AM
page loaded in 3697 ms. (0)

Similar Artilces:

PROC REPORT, grouping and counting
that > this is a silly method, but it is quick and dirty and that's typically > all the time I have to spend on an issue like this one, since I don't > do work for production but for my own analysis. thank you for your answer. I'm really a n00b to sas. Could you please point me to some example code realizing this? michael Michael, Just do a data step to the cntr var in your file on every record. Data new1; set infile; cntr = 1; run; quit; Then you can always count the variable cntr for groupings. You could even do it ahead of time if you need it that way...here

Creating Dummy Variables from Values of Variables (reformatted to
We have a dataset that has up to nine ICD-9 diagnosis codes in each record. We want to identify the top 50 (or 100 or 250) ICD-9 and construct dummy variables for the existence of those codes. Thanks to Dan Nordlund's reply to Trish Bous (at http://groups.google.com/group/comp.soft-sys.sas/browse_thread/thread/c82586e7f1164e54/ec5fc63a3eb16d9b?lnk=gst&q=proc+freq+order) we have the first part done (the identification of the top x codes and have saved them in a dataset. The first four observations of this dataset can be created with: data TopX; input code \$5. count; datalines;

Ntp configuration 61381
i have a standalone win2k3 server , i wish to configure against a ntpd on linux. I have configured the following regestery key's according to MS document : HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters\Type HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config\AnnounceFlags HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters\NtpServer HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient\SpecialPollInterval HKEY_LOC

All time top posters: 3228 clhupr...@aol.com 2327 p.salva...@verizon.net 1638 kelln...@cbd.net 1615 daledunnSCRA...@jamestool.com 1282 jer...@AtWorkCom.com 1234 z...@gte.net 1119 mwil...@sigmathree.com 1026 kellnerp2...@my-deja.com 993 cons...@engtran.com 982 mlomb...@frontiernet.net On 18 Jan 2006 18:17:23 -0800, "jon_banquer" <jon_banquer@yahoo.com> wrote: >All time top posters: Been busy removing your old posts from Google's archives? LOL ... -- Cliff On 18 Jan 2006 18:17:23 -0800, "jon_banquer" <jon_banquer@yahoo.com> w

help with creating an image cycler

Multiple lengths were specified for the BY variable
I'm trying to merge two datasets. I have a string variable. In both datasets they are defined as \$16. However in one the length is the length of the string and the other is 16. I've tried a few things such as compress and trim but I can't get the strings the same length so a merge using them as the by variable will work correctly. Willaim - Set the length of the variable before the set statement... Data toolong; Length longvar \$ 16; Set toolong; ..... This way the variable in the sas vector is set up before it is seen in the set statement. Paul Choate DDS Data Extraction (916) 654-2160 -----Original Message----- From: William Kossack [mailto:kossackw@NJC.ORG] Sent: Thursday, September 04, 2003 2:11 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Multiple lengths were specified for the BY variable I'm trying to merge two datasets. I have a string variable. In both datasets they are defined as \$16. However in one the length is the length of the string and the other is 16. I've tried a few things such as compress and trim but I can't get the strings the same length so a merge using them as the by variable will work correctly. Try

Can anybody tell me?? #2
how should I create dialog box for any mailbox program in shell scripts????????

set indexing( i+1) as a variable
I am having a problem. Say every time cl2 is greater than cl1 I want to know what cl1(i+1) is. This worked just like I thought it would. I did it below. cl1=[5;10;15;20;25;30] cl2=[6;11;14;21;24;31] for i = 1:length(cl1)-1 if cl2(i)>cl1(i) B=cl1(i+1) end end the output was B=10, B=15,B=25 My problem is that I would like to make the i+1 part of my code a variable, so I could change that part to be ( i+1) or (i+2) or just (i) or just about anything that works. So I tried to make the (i+1) part a variable but it does not seem to work. Below I made ii a variable and put it in the code where B=cl1(ii) ii=i+1 cl1=[5;10;15;20;25;30] cl2=[6;11;14;21;24;31] for i = 1:length(cl1)-1 if cl2(i)>cl1(i) B=cl1(ii) end end ??? Subscript indices must either be real positive integers or logicals. Error in ==> manipVect at 41 B=cl1(ii) This error message makes it seem like my indexing is wrong, but I know it worked before I tried to make ( i+1 ) into a variable so I think the way I am making i+1 into a variable is wrong. Could you tell me the right way to turn an index into a variable? Thank you

Creating a PrintWriter with BufferedWriter versus just File
What's the difference between this code: PrintWriter destination = new PrintWriter ( new BufferedWriter( new FileWriter( new File( arg)))); and this code: PrintWriter destination = new PrintWriter( new File( arg)); Do I get buffered output with the first code that I don't get with the second code? Or do I get the same buffered behavior with both? Kevin Simonson KevinSimonson wrote: > What's the difference between this code: >=20 > PrintWriter destination > =3D new PrintWriter > ( new BufferedWriter( new FileWrite

Passing Variables to standalone compiled matlab function
Hi, I have a matlab function that I have compiled into a standalone executable in windows. As a matlab function, the to inputs are 1 and directory information 'c:\data\dbase\', and I would use: functionname(1,'c:\data\dbase\'); when I call the function from a DOS shell I have tried: filename.exe 1 'c:\data\dbase\' but I get an error for too many input arguments. I have tried filename.exe 1 ['c:\data\dbase\'] with the same result. Please help. Thanks L. On 10-11-29 11:18 AM, ladidalimey wrote: > I have a matlab fu