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

I am working on a supermarket pos data. Multiple individuals bought various foods. 

 

Does anyone know how can i create sum each food by group by person?  

 

 example 🙂 

 

id    food    

1     milk 

1     milk 

1     egg

1     egg 

1     egg

1     soy 

2     milk 

2     egg 

2     egg 

2     milk 

.......

.......

 

 

like this 

 

id     food_milk      food_egg      food_soy 

1          2                   3                   1

2          2                    2                   0

3          1                    0                    3

4          2                    1                     2

 

.......

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

If you don't mind having a total column, the following might be your easiest method:

proc freq data=have;
  table id*food/nocol nocum nopercent norow;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

4 REPLIES 4
Reeza
Super User

First summarize the data into the numbers you want (PROC MEANS/SUMMARY) and then transpose the data (PROC TRANSPOSE).

 


@LeeSeongWoo_ wrote:

I am working on a supermarket pos data. Multiple individuals bought various foods. 

 

Does anyone know how can i create sum each food by group by person?  

 

 example 🙂 

 

id    food    

1     milk 

1     milk 

1     egg

1     egg 

1     egg

1     soy 

2     milk 

2     egg 

2     egg 

2     milk 

.......

.......

 

 

like this 

 

id     food_milk      food_egg      food_soy 

1          2                   3                   1

2          2                    2                   0

3          1                    0                    3

4          2                    1                     2

 

.......

 

 

 


 

kiranv_
Rhodochrosite | Level 12

If you have fewer values then use sql as shown below. else proc transpose is the way to go as suggested by @Reeza

proc sql;
select id, 
       sum(case when food='milk' then 1 else 0 end) as food_milk,
	   sum(case when food='egg' then 1 else 0 end) as food_egg,
	   sum(case when food='soy' then 1 else 0 end) as food_soy
from have
group by id;
LeeSeongWoo_
Fluorite | Level 6

Thank you so much for your help .

art297
Opal | Level 21

If you don't mind having a total column, the following might be your easiest method:

proc freq data=have;
  table id*food/nocol nocum nopercent norow;
run;

Art, CEO, AnalystFinder.com

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 850 views
  • 3 likes
  • 4 in conversation