BookmarkSubscribeRSS Feed
CathyVI
Pyrite | Level 9

Hello,

I would like to sum my data set and use a proc tabulate to get sum for 1's and 0's. 

For example:

data Have;
input A B C D;
datalines;
1 0 1 1
0 1 1 1
1 1 0 0;
run;

I used the code below but its only gives me value when total =1 or 0, not the value of all totals.

data want;
set have;
if a=1 | b=1 |c=1|d=1 then total=1; else total=0;
run;

I would like my output to have total of a,b,c,d when its 1, and when its 0.

For example

Output

TotalN%
1866.6
0433.4
All12100

 

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Sum rows, or sum columns?

 

data want;
set have;
if a=1 | b=1 |c=1|d=1 then total=1; else total=0;
run;


This isn't a sum at all.

 

I shouldn't have to guess or ask. Do you want the total number of 1s and the total number of zeros? You don't say this at all.

--
Paige Miller
CathyVI
Pyrite | Level 9

@PaigeMiller  I want the total number of 1s and the total number of zeros. This is what I showed in my output table. The table shows total number of 1's and 0's. This is how I want my output to look like. Thanks

Kurt_Bremser
Super User
data long;
set have;
array all {*} a--d;
do i 0 1 to dim(all);
  x = all{i};
  output;
end;
keep x;
run;

proc freq data=long;
tables x;
run;
CathyVI
Pyrite | Level 9

@Kurt_Bremser There was an error when i ran your code


61 data long;
62 set have;
63 array all {*} a--d;
64 do i 0 1 to dim(all);
-
73
200
ERROR 73-322: Expecting an =.

ERROR 200-322: The symbol is not recognized and will be ignored.

65 x = all{i};
66 output;
67 end;
68 keep x;
69 run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.LONG may be incomplete. When this step was stopped there were 0
observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds

 

Reeza
Super User
data Have;
input A B C D;
datalines;
1 0 1 1
0 1 1 1
1 1 0 0
;
run;

data long;
set have;

array vars(4) a b c d;

do i=1 to dim(vars);
   value = vars(i);
   var = vname(vars(i));
   output;
end;


keep var value;
run;

proc freq data=long;
table value;
run;



  • Transpose your data to a long format
  • Use PROC FREQ to summarize. 

Another method may be to summarize and then collate the summary. 

This seemed easier but both work.

 


@CathyVI wrote:

Hello,

I would like to sum my data set and use a proc tabulate to get sum for 1's and 0's. 

For example:

data Have;
input A B C D;
datalines;
1 0 1 1
0 1 1 1
1 1 0 0;
run;

I used the code below but its only gives me value when total =1 or 0, not the value of all totals.

data want;
set have;
if a=1 | b=1 |c=1|d=1 then total=1; else total=0;
run;

I would like my output to have total of a,b,c,d when its 1, and when its 0.

For example

Output

Total N %
1 8 66.6
0 4 33.4
All 12 100

 


 

Tom
Super User Tom
Super User

Assuming you meant to read in all three lines of data and not just the two your posted code was reading:

data have;
  input total @@;
datalines;
1 0 1 1
0 1 1 1
1 1 0 0
;

proc freq ;
  tables total;
run;

Result

Tom_0-1678744506257.png

 

Tom
Super User Tom
Super User

If you are really stuck with data in that form then you can count them yourself.

data have;
  input v1-v4;
datalines;
1 0 1 1
0 1 1 1
1 1 0 0
;

data want;
  set have end=eof;
  array all _numeric_;
  ones+sum(of all[*]);
  zeros+dim(all)-sum(of all[*]);
  if eof then do;
    total=1; n=ones; p=ones/(ones+zeros); output;
    total=0; n=zeros;  p=zeros/(ones+zeros);output;
  end;
  keep total n p ;
run;

proc print;
 sum n;
run;

Tom_0-1678745604082.png

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 848 views
  • 0 likes
  • 5 in conversation