BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

data have;
input mem $ frequency percent;
datalines;
M 240 24.0
M 320 32.0
F 120 12.0
M 80 8.0
C 68 6.8
M 42 4.2
C 130 13.0
;
run;

HOW TO DO SUM BY MEM(GROUP VARIABLE) WISE IN DATA STEP BLOCK?

GROUP VARIABLE IS MEM LIKE SEE BELOW 

EX1:

mem

frequency

percent

C

68

6.8

C

130

13

C

198

19.8

F

120

12

M

42

4.2

M

80

8

M

240

24

M

320

32

M

682

68.2

 

EX2:

mem

frequency

percent

C

68

6.8

C

130

13

TOTAL

198

19.8

F

120

12

M

42

4.2

M

80

8

M

240

24

M

320

32

TOTAL

682

68.2

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

I don't understand what your desired result would look like?

 

Though probs for providing sample data in the form of a data step! 🙂

novinosrin
Tourmaline | Level 20

@PeterClemmensen OP wants the by group sum in green

mem

frequency

percent

C

68

6.8

C

130

13

C

198

19.8

F

120

12

M

42

4.2

M

80

8

M

240

24

M

320

32

M

682

68.2

 

EX2:

mem

frequency

percent

C

68

6.8

C

130

13

TOTAL

198

19.8

F

120

12

M

42

4.2

M

80

8

M

240

24

M

320

32

TOTAL

682

68.2

to be computed using datastep and output to 2 different datasets one having the group's name at the end of each by group and the other having Total as the label. Straight forward retain(sum statement) and reinitialise to zero at the top of each by group. I am just too lazy, may be you can chime in 🙂

Reeza
Super User

@novinosrin Or go back a few steps and get the totals from whatever proc generated the freq/percent in the first place  rather than manually modify it. Just a caution with adding percents, you can't always just 'add' them depending on the context and interpretation.

 

 

novinosrin
Tourmaline | Level 20

@Reeza Absolutely. That's great attention to detail. Nice.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 1127 views
  • 2 likes
  • 4 in conversation