DATA Step, Macro, Functions and more

row wise count

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

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

 

IDsVar1Var2Var3Var4Var5var6Var7Count_1
10000111 
20000011 
31000000 
40010000 
50101010 
60000001 
70011100 
80000000 
90000000 
100000000 
110000000 
120000000 
130001010 
140011100 
151100000 
160000111 
170000000 
180000000 
190010000 
200100010 
210000000 
221000010 
230011000 

 

data want :

 

IDsVar1Var2Var3Var4Var5var6Var7Count_1
100001113
200000112
310000001

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

Re: row wise count

Posted in reply to soham_sas

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

View solution in original post


All Replies
Super User
Super User
Posts: 9,211

Re: row wise count

Posted in reply to soham_sas

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: 9,593

Re: row wise count

Posted in reply to soham_sas
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: 37

Re: row wise count

Posted in reply to KurtBremser
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...
PROC Star
Posts: 1,190

Re: row wise count

Posted in reply to soham_sas

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: 9,593

Re: row wise count

Posted in reply to soham_sas

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
Contributor
Posts: 37

Re: row wise count

Posted in reply to KurtBremser

@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: 9,593

Re: row wise count

Posted in reply to soham_sas

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
Super User
Posts: 9,211

Re: row wise count

Posted in reply to soham_sas

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: 81

Re: row wise count

Try this one...

 


data want;
set have;
a1=n(of varSmiley Happy;
run;

Super User
Super User
Posts: 9,211

Re: row wise count

Posted in reply to rajeshalwayswel

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.

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 208 views
  • 5 likes
  • 5 in conversation