Pyrite | Level 9

## Create sum of binary integers

Hi,

I wish to sum binary integers and create a new var as the sum of each record. A percentage of each type of occurrence is then computed. An example is shown below.

'var1' is an available binary integer of type numeric. 'sum_var1' is to be computed from each binary integer (var1). 'counts of n' is the number of occurrences in sum_var1 of 1, 2, 3, etc. 'total counts' is the value given in Properties-->Advanced-->Rows for the table of interest.

 var1 sum_var1 1000 1 100 1 100 1 100010 2 100011 3 1 1 100 1 1000 1 100001 2 11000 2 1 1 100001 2 100000 1 10000 1 101000 2 counts of 1 9 % of 1 60 counts of 2 5 % of 2 33.33333 counts of 3 1 % of 3 6.666667 total counts 15

Thanks.

capam

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Create sum of binary integers

Thanks for the clarification. So, your integers are in fact decimal integers (which happen to consist of 0s and 1s)?

Then you don't need formats or informats for binary numbers:

``````data Slip_Slide_Incipient_ON_sum;
set Slip_Slide_Incipient_ON_sum;
SS_sum = countc(put(Slip_Slides_DP, 6.),'1');
run;``````
5 REPLIES 5

## Re: Create sum of binary integers

Hi @capam,

Try this:

``````data have;
input var1 binary6.;
cards;
1000
100
100
100010
100011
1
100
1000
100001
11000
1
100001
100000
10000
101000
;

data want;
set have;
sum_var1=countc(put(var1, binary6.),'1');
run;

proc freq data=want;
tables sum_var1;
run;``````
Pyrite | Level 9

## Re: Create sum of binary integers

Thanks Free for the quick response.

I get the following:

 var1 sum_var1 1000 2 100 2 100 2 100010 3 100011 4 1 1 100 2 1000 2 100001 2

It should be

 var1 sum_var1 1000 1 100 1 100 1 100010 2 100011 3 1 1 100 1 1000 1 100001 2

capam

## Re: Create sum of binary integers

@capam wrote:

Thanks Free for the quick response.

I get the following:

(...)

Please show your code together with usable input data. My code gives the desired result based on the HAVE dataset created from data lines.

Pyrite | Level 9

## Re: Create sum of binary integers

Typical data:

1000
100
100
100010
100011
1
100
1000
100001
11000
1
100001
100000
10000
101000
100000
11
1

Type is numeric, length is 8. code is below.

data Slip_Slide_Incipient_ON_sum;
set Slip_Slide_Incipient_ON_sum;
SS_sum = countc(put(Slip_Slides_DP, binary6.),'1');
run;

## Re: Create sum of binary integers

Thanks for the clarification. So, your integers are in fact decimal integers (which happen to consist of 0s and 1s)?

Then you don't need formats or informats for binary numbers:

``````data Slip_Slide_Incipient_ON_sum;
set Slip_Slide_Incipient_ON_sum;
SS_sum = countc(put(Slip_Slides_DP, 6.),'1');
run;``````
Discussion stats
• 5 replies
• 1457 views
• 0 likes
• 2 in conversation