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

Hi All,

 

I have two tables. I need to join them and connect the columns. Below are the two source tables and the third table is the desired table.

 

Table 1:

date        units        loss

jan17         1            $10

feb17         2              8

march17     4             11

 

Table 2:

march17      2          10 

apr17           2         10

may17         1          19

 

 

Desired table:

jan17       1              10

feb17      2               8

march17  6             21

apr17       2             10

may17       1            19

 

Any help will be greatly appreciated. Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data table1;
input date $        units        loss;
datalines;
jan17         1            10
feb17         2              8
mar17     4             11
;

 
data table2;
input date $        units        loss;
datalines;
mar17      2          10 
apr17           2         10
may17         1          19
;

proc sort data=table1;
by date;
run;

proc sort data=table2;
by date;
run;


data want;
set table1 table2;
by date;
if first.date then do;sumunits=0;sumloss=0;end;
sumunits+units;sumloss+loss;
if last.date;
run ;

View solution in original post

4 REPLIES 4
Astounding
PROC Star

Once you put the tables together, it's just a matter of adding up numbers.  For example:

 

data table3;

set table1 table2;

run;

 

proc summary data=table3 nway;

var units loss;

class date;

output out=want (drop=_type_ _freq_) sum=;

run;

 

proc print data=want;

run;

 

*** EDITED:  Sorry, I should have checked this more carefully.  Notice the added statement.

ndee
Calcite | Level 5

Thanks for replaying. 

 

By doing this: data table3;
                      set table1 table2;
                      run;

table 3:
jan17        1      10
feb17        2       8
march17   4        11
march17    2         10
apr17         2      10
may17        1       19

 

 

And the proc summary step is summing up all the values and giving out just one observation:

 

N        loss

12       68

 

The output I wanted was a table with march being added up like this:

jan17        1      10
feb17        2       8
march17   6        21
apr17         2      10
may17        1       19

 

 

 

novinosrin
Tourmaline | Level 20
data table1;
input date $        units        loss;
datalines;
jan17         1            10
feb17         2              8
mar17     4             11
;

 
data table2;
input date $        units        loss;
datalines;
mar17      2          10 
apr17           2         10
may17         1          19
;

proc sort data=table1;
by date;
run;

proc sort data=table2;
by date;
run;


data want;
set table1 table2;
by date;
if first.date then do;sumunits=0;sumloss=0;end;
sumunits+units;sumloss+loss;
if last.date;
run ;
ndee
Calcite | Level 5
That worked. Thank you 🙂

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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