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

Hi everyone,

 

The challenge: I have the sales and region of each store. I wanna calculate each store's percentage sales of its region's total and add this result as a new variable in the dataset.

 

What I have so far: I'm assuming this is solvable by PROC SQL yet, as you can probably guess  now, my SQL skills are newbie-level. I only got this far (from another thread here😞

 

PROC SQL;

SELECT Region, COUNT(storeID), SUM(Sales)
FROM sample 
GROUP BY Region
ORDER BY Region;

QUIT;

 

Any help would be much appreciated.

 

I'm attaching a sample of the data for your convenience. I'm using SAS 9.4.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Since you are adding the variable to the data set, I would go with a DATA step.  As long as each store has a single observation, there are no complications. 

 

proc sort data=have;

by region;

run;

 

data want;

tot_sales=0;

do until (last.region);

   set have;

   by region;

   tot_sales + sales;

end;

do until (last.region);

   set have;

   by region;

   pct_sales = sales / tot_sales;

   output;

end;

drop tot_sales;

run;

View solution in original post

3 REPLIES 3
Reeza
Super User

Try PROC FREQ instead with the default output data set.

Astounding
PROC Star

Since you are adding the variable to the data set, I would go with a DATA step.  As long as each store has a single observation, there are no complications. 

 

proc sort data=have;

by region;

run;

 

data want;

tot_sales=0;

do until (last.region);

   set have;

   by region;

   tot_sales + sales;

end;

do until (last.region);

   set have;

   by region;

   pct_sales = sales / tot_sales;

   output;

end;

drop tot_sales;

run;

mragaa
Obsidian | Level 7

Thank you @Astounding and @Reeza , for your time and brain cycles.

 

@Astounding  Awesome! I really appreciate that you wrote this beautiful code. It works fine and I'll spend some time "studying" it. Thank you.

 

@ReezaThank you for this suggestion. I'll definitely look into it while reverse engineering Astounding's code.

 

 

My respects!

 

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 987 views
  • 2 likes
  • 3 in conversation