BookmarkSubscribeRSS Feed
TT_123
Calcite | Level 5

Hi,

Just wondering if anyone would be able to help with the SAS PROC SQL codes please?

 

  • Raw data sample in Table 1
  • I've to summarise the amount for different Product Types (Table 2) - both at a department level (Table 3) and a section level (Table 4).  
  • Both department and section lists are dynamic - they change every month.  I've to refer to these dynamic tables for summarisation.
  • A sample of expected end results in (Table 5a and 5b)

Would someone know what's the SAS PROC SQL codes to summarise by referring to different dynamic tables please?

 

Thanks.

 

Raw Data sample (Table 1)

 

DepartmentSectionProduct TypeAmount
Department ASection D1011,000,000
Department CSection B1052,000,000
Department BSection E1023,000,000
Department ESection B1034,000,000
Department DSection A1045,000,000
Department BSection C1029,000,000
Department CSection D1018,000,000
Department ASection B1035000,000
Department ESection E1047,000,000
Department BSection D1021,500,000

 

 

Table 2 - a list of products that require summarising at both a department and a section level.

Product Type
101
102
103
104
105

 

Table 3 - a list of latest department names (dynamic, changes each month)

Department Name
Department A
Department B
Department C
Department D
Department E

 

 

Table 4 - a list of latest section names (dynamic, changes each month)

Section Name
Section A
Section B
Section C
Section D
Section E

 

Table 5a - expected results, summarising by department

Department NameProduct TypeAmount
Department A101500,000
Department A102600,000
Department A103700,000
Department A104800,000
Department A105900,000
Department B101120,000
Department B102320,000
Department B103240,000
Department B104630,000
Department B105812,000

 

Table 5b - expected results - summarising by section

Section NameProduct TypeAmount
Section A10140,000
Section A10280,000
Section A10330,000
Section A10420,000
Section A10570,000
Section B10190,000
Section B10280,000
Section B10310,000
Section B10458,000
Section B10591,000

 

 

 

2 REPLIES 2
PaigeMiller
Diamond | Level 26

I can't imagine how you get the results where all amounts are less than 1,00,000 when the input data has all amounts in the millions. Please explain further.

 

Is the data dynamic and changes every month along with the department and section lists? Are there departments in the data that do not appear in the department list? Are there sections in the data that do not section in the department list?

 

Please do not type in ALL CAPITAL letters, as you did in the title.

--
Paige Miller
ballardw
Super User

Why do you specify that Proc SQL has to be used?

I suspect that Proc Summary will generate the needed result in one pass. Select the appropriate _TYPE_ value if you need them split out.

 

Consider this example. You should have the SAS supplied data set SASHELP.CARS in your installation so the code would run and generate output.

Proc summary data=sashelp.cars;
   class   origin make type;
   var   msrp;
   output out=work.summary sum=;
run;

When you examine the output data set, WORK.SUMMARY, the summary will have the sum of the variable MSRP by overall, each level of the variables Origin, Make and Type alone and in combinations of 2 or 3 at a time the. Variable _TYPE_ can be used to subset the result as needed.

IF you only need specific values of any of the Class variables you can use a WHERE clause to restrict them but the way you asked your question is not clear whether the "lists" are specified or if you want all the values  that may change.

An additional advantage of Proc Summary over Proc SQL is if you need other variables summarized you just add the variable to the VAR statement. Also if additional statistics are needed, such as Min and Max, you can add the option /autoname to the output line and SAS will create variable names with the statistic appended. With Proc SQL you would have to write some possibly very complex code to create variable names.

Example requesting different statistics for different variables

Proc summary data=sashelp.cars;
   class   origin make type;
   var   msrp invoice horsepower weight ;
   output out=work.summary2 
       sum(msrp invoice) =  min(horsepower weight)= max(horsepower weight)=
       mean(horsepower weight)= /autoname
;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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