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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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