Solved
Contributor
Posts: 49

# row wise count

Hi

I have a data set like below , i want the number of 1 present from var1 to var7 in the count_1 column

E.g Id 1 will have 3 , Id 2 will have 2 and so on ...

data have : ..

 IDs Var1 Var2 Var3 Var4 Var5 var6 Var7 Count_1 1 0 0 0 0 1 1 1 2 0 0 0 0 0 1 1 3 1 0 0 0 0 0 0 4 0 0 1 0 0 0 0 5 0 1 0 1 0 1 0 6 0 0 0 0 0 0 1 7 0 0 1 1 1 0 0 8 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 13 0 0 0 1 0 1 0 14 0 0 1 1 1 0 0 15 1 1 0 0 0 0 0 16 0 0 0 0 1 1 1 17 0 0 0 0 0 0 0 18 0 0 0 0 0 0 0 19 0 0 1 0 0 0 0 20 0 1 0 0 0 1 0 21 0 0 0 0 0 0 0 22 1 0 0 0 0 1 0 23 0 0 1 1 0 0 0

data want :

 IDs Var1 Var2 Var3 Var4 Var5 var6 Var7 Count_1 1 0 0 0 0 1 1 1 3 2 0 0 0 0 0 1 1 2 3 1 0 0 0 0 0 0 1

Accepted Solutions
Solution
‎11-28-2017 08:22 AM
Super User
Posts: 10,574

## Re: row wise count

You are not counting observations, you are counting through variables within an observation.

The number of iterations of the do loop is determined by the size of the array, which depends on the number of variables named in the array statement. Basically

`-var7`

in the array statement sets all this.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Super User
Posts: 9,840

## Re: row wise count

Not tested as not typing that in - post test data in th eform of a datastep in future.

```data want;
set have;
count_1=sum(of var:);
run;```
Super User
Posts: 10,574

## Re: row wise count

``count_1 = sum(of var1-var7);``
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 49

## Re: row wise count

Hi.
All the observations are 1 so sum function will work. If the observation
are not 1 or any character variable. Then how to perform the count?

PROC Star
Posts: 1,401

## Re: row wise count

Then put your variables into an array, loop over the array and set a counter to count=count+1 when the variable value meets some criterion..

Super User
Posts: 10,574

## Re: row wise count

soham_sas wrote:
Hi.
All the observations are 1 so sum function will work. If the observation
are not 1 or any character variable. Then how to perform the count?

Define an array for your existing variables, and count in a do loop.

``````array vars{*} var1-var7;
counter_1 = 0;
do i = 1 to dim(vars);
if not missing(vars{i}) then counter_1 + 1;
end;
drop i;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 49

## Re: row wise count

@KurtBremserand @draycut thanks for the solutions i tried the below code and its working fine , but i am not getting the logic that how SAS is counting the number of observation as there is no count option/function is used , please explain how the "counter_1 + 1" in the below code the giving the count of observations

data want;
set ds1;
array vars{*} var1-var7;
counter_1 = 0;
do i = 1 to dim(vars);
if (vars{i})=1 then counter_1 + 1;
end;
drop i;
run;

Solution
‎11-28-2017 08:22 AM
Super User
Posts: 10,574

## Re: row wise count

You are not counting observations, you are counting through variables within an observation.

The number of iterations of the do loop is determined by the size of the array, which depends on the number of variables named in the array statement. Basically

`-var7`

in the array statement sets all this.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 9,840

## Re: row wise count

I have no idea what your posts mean?  From your original test data output, the sum should work.  If it doesn't post test data in the form of a datastep which illustrates your problem, and what the result should be.

Frequent Contributor
Posts: 93

Try this one...

data want;
set have;
a1=n(of var;
run;

Super User
Posts: 9,840