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
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;
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;
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
@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.
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;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.