I am trying to sum each line of data that I have below and create a histogram from the information, but I don't know how to approach this. For example, in line 1, I have the values 1 and 1 so I want to add 1+1 = 2, and then 1 + 2 = 3, and so on and so forth and then plot a histogram of the frequencies of each sum. I will appreciate any help with this. Thank you.
EDIT: I updated the variables for both to be numeric, but I want the frequencies to be the frequency of each sum expressed as a percentage of the total. For example, the sum 2 only shows up once when you toss two dice, so the percentage is 1/36 or 2.78%. I tried to do sum_dices = sum(dice1, dice2)/36; but it just changed the x-axis labels without changing the data.
DATA auto;
INPUT dice1 dice2;
sum_dices = sum(dice1, dice2);
CARDS;
1 1
1 2
1 3
1 4
1 5
1 6
2 1
2 2
2 3
2 4
2 5
2 6
3 1
3 2
3 3
3 4
3 5
3 6
4 1
4 2
4 3
4 4
4 5
4 6
5 1
5 2
5 3
5 4
5 5
5 6
6 1
6 2
6 3
6 4
6 5
6 6
;
proc sgplot data=auto;
VBAR sum_dices;
run;
Why have you defined dice1 as character ($) and not numeric ?
Don't you mean to sum: dice1 + dice2 ?
You should, first create a new variable wit the sum result, then in the next step run the graphic procedure.
DATA auto;
INPUT dice1 dice2; /* both variables numeric */
sum_dices = sum(dice1 dice2);
CARDS;
1 1
1 2
1 3
1 4
1 5
1 6
2 1
2 2
......
;
run;
proc sgplot data=auto;
HBAR sum_dices;
run;
Why have you defined dice1 as character ($) and not numeric ?
Don't you mean to sum: dice1 + dice2 ?
You should, first create a new variable wit the sum result, then in the next step run the graphic procedure.
DATA auto;
INPUT dice1 dice2; /* both variables numeric */
sum_dices = sum(dice1 dice2);
CARDS;
1 1
1 2
1 3
1 4
1 5
1 6
2 1
2 2
......
;
run;
proc sgplot data=auto;
HBAR sum_dices;
run;
Something like this?
Also one of the variables is defined as a character variable, howcome?
data WANT;
set HAVE;
call missing (TOTAL);
TOTAL = sum(DICE1, DICE2);
run;
proc sgplot data=WANT;
vbar TOTAL ;
run;
@VRKiwi wrote:
Something like this?
Also one of the variables is defined as a character variable, howcome?
data WANT; set HAVE; call missing (TOTAL); TOTAL = sum(DICE1, DICE2); run; proc sgplot data=WANT; vbar TOTAL ; run;
You don't need to set a variable to missing before you use it. The variable will be set to missing automatically in each iteration of set statement.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.