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

Hi! 

I have to compare if 2 fields from 2 different tables are the same or at least, see how much the difference is.

 

I have var "price1" from table1 and var "price2" from table2. Both tables in the same library.

 

How can I print the comparision between price1 and price2 in one only output like this:

price1price2difference
550

 

thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@clipsia wrote:

sorry but Im talking about huge tables.

I just need sum(price1) because is the total sum of the variable that Im comparing


Then you summarize the data with a procedure like Proc Means or summary, then combine.

 

Proc summary data=have;
   var price1;
   output out=work.price1sum  sum=;
run;

will create a data set with the sum or Price1 (and couple of other variables you can ignore for the moment).

Do the same with the other data set. Then combine.

View solution in original post

3 REPLIES 3
ballardw
Super User

Combine the data into one data set then do the comparison.

If there is a variable or combination of variables to match the two on that makes more sense.

data one;
  input id price1;
datalines;
1  4
2  5
3  77
;

data two;
  input id price5;
datalines;
1 1
2 15
3 12
;

data want;
   merge one two;
   by id;
   difference = price1 - price5;
run;

proc print data=want;
   var price1 price5 difference;
run;

If you only have one record then the merge still works. If there are multiple records and you want to match them on a variable(or more) then sort by the matching variable prior to the data step with the merge.

 

There are other ways possible as well. Example data of more complex problems would be needed for any solution.

clipsia
Fluorite | Level 6

sorry but Im talking about huge tables.

I just need sum(price1) because is the total sum of the variable that Im comparing

ballardw
Super User

@clipsia wrote:

sorry but Im talking about huge tables.

I just need sum(price1) because is the total sum of the variable that Im comparing


Then you summarize the data with a procedure like Proc Means or summary, then combine.

 

Proc summary data=have;
   var price1;
   output out=work.price1sum  sum=;
run;

will create a data set with the sum or Price1 (and couple of other variables you can ignore for the moment).

Do the same with the other data set. Then combine.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 792 views
  • 1 like
  • 2 in conversation