DATA Step, Macro, Functions and more

Summing values of a variable in proc tabulate

Reply
Contributor
Posts: 23

Summing values of a variable in proc tabulate

[ Edited ]

Hello,

 

I have a variable x  which is a binary variable (may have missing values). A sum function in proc means would give me value of 60. (yes=60,  no or missing=40).

When I use the sum function in proc tabulate with a class statement, I get the number of obs (n=100) instead of the sum (60).

 

Here is the code I am using:

 

proc tabulate data = have;

var x;

class y;

table x*sum, y;

run;

 

Any help would be appreciated.

 

Thanks

Super User
Posts: 13,950

Re: Summing values of a variable in proc tabulate

Show your output or provide the data used in the form of a data step, preferably both.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Super User
Posts: 24,027

Re: Summing values of a variable in proc tabulate

Your code works fine for me. You have some other issue. 

If you run the following the results should be the following. I added in the Count/N so you can see the difference and the PROC MEANS to show that the results are correct.  

 

 
  y
0 1
x Sum 20.00 12.00
x N 56 44

 

data have;
    call streaminit(12345);

    do i=1 to 100;
        x=rand('bernoulli', 0.4);
        y=rand('bernoulli', 0.5);
        output;
    end;
run;

proc tabulate data=have;
    var x;
    class y;
    table x*sum x*n, y;
run;

proc means data=have n sum nway;
    var x;
    class y;
run;

div44 wrote:

Hello,

 

I have a variable x  which is a binary variable (may have missing values). A sum function in proc means would give me value of 60. (yes=60,  no or missing=40).

When I use the sum function in proc tabulate with a class statement, I get the number of obs (n=100) instead of the sum (60).

 

Here is the code I am using:

 

proc tabulate data = have;

var x;

class y;

table x*sum, y;

run;

 

Any help would be appreciated.

 

Thanks


 

Ask a Question
Discussion stats
  • 2 replies
  • 103 views
  • 0 likes
  • 3 in conversation