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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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