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

Hello guys,

 

can you please advise with the following?

I need to combine multiple tables based on list of values with the following structure:

 

Table1 Table2 Desired output
Col1 Col2Col3 Col1Col4
A BBB5 A90
B CCC10 B40
C AA15 C10
  AAAA20   
  AAA25   
  A30   
  B135   

 

that means that in table1 I have list of values and I need to sum quantity from Col3 but there is no exact match with Col1 and Col2.

 

Thanks in advance

Jan

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

How about this one ?

 

 

 

 

data Table1;
input Col1 $40.;
datalines;
Americas
Americas_US
Americas_US_West
Americas_Canada
Americas_Canada_North
;

data Table2;
input Col2 : $40. Col3;
datalines;
Americas_US_East_Sales	10
Americas_US_East_CS	20
Americas_US_East_Sales_Team	30
Americas_Canada	40
Americas_US_West	50
;

proc sql;
create table want as
 select Col1,coalesce(sum(Col3),0) as sum
  from table1 as a left join table2 as b
   on Col2 like cats(Col1,'%')
    group by Col1;
quit;

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

Assuming your example data is representative, do something like this

 

data Table1;
input Col1 $ @@;
datalines;
A B C
;

data Table2;
input Col2 $ Col3;
datalines;
BBB 5
CCC 10
AA 15
AAAA 20
AAA 25
A 30
B1 35
;

proc sql;
   create table want as
   select Col1
         ,sum(Col3) as Col4
   from Table1, Table2
   where Col1=substr(compress(Col2, , 'd'), 1, 1)
   group by Col1;
quit;
bon0
Calcite | Level 5

Thank you but is there some way how to do this dynamically as the list of data and the results can change and I need to replicate similar or more complex logic for multiple tables.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please supply test data/required output which illustrates the problem, theoretical problems are very hard to solve 🐵

PeterClemmensen
Tourmaline | Level 20

You have to be more specific than this. How is the logic more complex in your actual problem? 

bon0
Calcite | Level 5

Suppose we have the following tables:

 

Table 1 - list of regions - exported from the source, name of regions might change:

Table1
Col1
Americas
Americas_US
Americas_US_West
Americas_Canada
Americas_Canada_North

 

Table 2 - list of regions with monthly product sales

 

Table 2
Col2Quantity
Americas_US_East_Sales10
Americas_US_East_CS20
Americas_US_East_Sales_Team30
Americas_Canada40
Americas_US_West50

 

Desired output should be table which aggregates the sales per regions and subregions based on the list from Table1

= sum quantity from table 2 where col2 contains value from col1...

Desired Output - Table 3
Col1Quantity
Americas150
Americas_US110
Americas_US_West50
Americas_Canada40
Americas_Canada_North0
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, from the specific example (and please note how I put test data in the form of a datastep!):

data table1;
  length col1 $200;
  input Col1 $;
datalines;
Americas
Americas_US
Americas_US_West
Americas_Canada
Americas_Canada_North
;
run;
data table2;
  length col2 $200;
  input Col2  $ Quantity;
datalines;
Americas_US_East_Sales   10
Americas_US_East_CS   20
Americas_US_East_Sales_Team   30
Americas_Canada   40
Americas_US_West   50
;
run;

proc sql;
  alter table table1 add quantity num;
  update table1
  set quantity=(select sum(this.quantity) from table2 this where substr(this.col2,1,lengthn(col1))=col1);
quit;

 

Ksharp
Super User

How about this one ?

 

 

 

 

data Table1;
input Col1 $40.;
datalines;
Americas
Americas_US
Americas_US_West
Americas_Canada
Americas_Canada_North
;

data Table2;
input Col2 : $40. Col3;
datalines;
Americas_US_East_Sales	10
Americas_US_East_CS	20
Americas_US_East_Sales_Team	30
Americas_Canada	40
Americas_US_West	50
;

proc sql;
create table want as
 select Col1,coalesce(sum(Col3),0) as sum
  from table1 as a left join table2 as b
   on Col2 like cats(Col1,'%')
    group by Col1;
quit;
bon0
Calcite | Level 5

Is there some way to avoid the datalines as it has to be done manually, right and it is not what I need as I want to avoid any manual input to the code in future...

RW9
Diamond | Level 26 RW9
Diamond | Level 26

The datalines step is there to show test data that we can run on our machines.  We do not have access to your computer, or data, so it has to be created by us, providing datastep with datalines provides a means for us to have some data to work with, which is essential considering SAS is a data based language:
https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 9 replies
  • 1044 views
  • 0 likes
  • 4 in conversation