turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- row wise count

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-28-2017 06:51 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to soham_sas

11-28-2017 07:56 AM

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

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to soham_sas

11-28-2017 06:55 AM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to soham_sas

11-28-2017 06:57 AM

`count_1 = sum(of var1-var7);`

---------------------------------------------------------------------------------------------

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

11-28-2017 07:01 AM

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?

Please help...

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?

Please help...

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to soham_sas

11-28-2017 07:09 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to soham_sas

11-28-2017 07:09 AM

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?

Please help...

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

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

11-28-2017 07:48 AM

@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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to soham_sas

11-28-2017 07:56 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to soham_sas

11-28-2017 07:56 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-28-2017 08:40 AM

Try this one...

data want;

set have;

a1=n(of var;

run;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to rajeshalwayswel

11-28-2017 08:41 AM

As a tip, use the code window (its the {i} above the post area) to preserve formatting - i.e. would of avoided the smiley face in your code.