BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
soham_sas
Quartz | Level 8

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
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
soham_sas
Quartz | Level 8
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...
PeterClemmensen
Tourmaline | Level 20

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

Kurt_Bremser
Super User

@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;
soham_sas
Quartz | Level 8

@Kurt_Bremserand @PeterClemmensen 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;

Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

rajeshalwayswel
Pyrite | Level 9

Try this one...

 


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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 5185 views
  • 7 likes
  • 5 in conversation