BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Fara_I
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

 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;

View solution in original post

3 REPLIES 3
Shmuel
Garnet | Level 18

 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;
VRKiwi
Obsidian | Level 7

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; 
   

 

 

andreas_lds
Jade | Level 19

@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: 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
  • 3 replies
  • 1002 views
  • 0 likes
  • 4 in conversation