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

Hi,

I am having the hardest time trying to figure out the SAS code to do the following: Assume I have two datasets (A) and (B)

(A)

ProductPrice
A$3
B 
C$5
D$8
E 

 

(B)

ProductPriceQuantity
A$31
B$102
E$124

 

I would like to left join (A) with (B) to get quantity (when it is available) and if price is missing in (A) then I would like to take the price from (B).

So, the dataset I would like to end up with looks like this:

 

(C)

ProductPriceQuantity
A$31
B$102
C$5 
D$8 
E$124

 

Thanks in advance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Looks like something you can do easily with normal SAS statements.

In this case the UPDATE statement.  You want to update the dataset with product/price lists with the information from the dataset that also has quantity information.

data prices;
  input Product $ Price :comma.;
cards;
A $3
B .
C $5
D $8
E .
;
 
data quantities ;
  input Product $ Price :comma. Quantity;
cards;
A $3 1
B $10 2
E $12 4
;

data want;
  update prices quantities;
  by product;
run;

Result

Tom_0-1728406279476.png

You did not show any situations where the product was in both dataset, but in that case any non-missing value of price in the transaction dataset (the one with the quantity variable) would replace the price from the original dataset (the one only the product and price).

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Looks like something you can do easily with normal SAS statements.

In this case the UPDATE statement.  You want to update the dataset with product/price lists with the information from the dataset that also has quantity information.

data prices;
  input Product $ Price :comma.;
cards;
A $3
B .
C $5
D $8
E .
;
 
data quantities ;
  input Product $ Price :comma. Quantity;
cards;
A $3 1
B $10 2
E $12 4
;

data want;
  update prices quantities;
  by product;
run;

Result

Tom_0-1728406279476.png

You did not show any situations where the product was in both dataset, but in that case any non-missing value of price in the transaction dataset (the one with the quantity variable) would replace the price from the original dataset (the one only the product and price).

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!

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