BookmarkSubscribeRSS Feed
tyq1992
Calcite | Level 5

Hi all,

 

I have two different datasets, 1 containing country and continent information and the other 1 containing continent and profit ratios.

 

Dataset 1:

Country          Continent

China              Asia

Japan             Asia

Malaysia         Asia

Germany        Europe

Australia         Oceania

 

Dataset 2:

Continent          Profit_Margin

Asia                  0.05

Europe             0.08

America            0.02

 

My desired results are:

Country          Continent         Profit_Margin

China              Asia                 0.05

Japan             Asia                  0.05

Malaysia         Asia                 0.05

Germany        Europe            0.08

Australia         Oceania          .

 

Have tried the following code:

Data want;

merge Dataset_1 (IN=A) Dataset_2 (IN=B);

by Continent;

If A;

 

But the results i get are:

 

Country          Continent         Profit_Margin

China              Asia                 0.05

Japan             Asia                  .

Malaysia         Asia                 .

Germany        Europe            0.08

Australia         Oceania          .

 

Appreciate any advice i could get on this. Thanks in advance!

 

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

1. Your code if fine is the data is sorted and works are desired

2. Your second data set is not sorted.

3. What do you have on your log?

tyq1992
Calcite | Level 5

Thanks.

 

Have sorted the second dataset by continent. It now appears as:

 

Dataset 2:

Continent          Profit_Margin

America            0.02

Asia                  0.05

Europe             0.08

 

Still facing the same problem though.

 

Log seems fine. It reads:

"There were 5 obs read from data set work.want"

 

gamotte
Rhodochrosite | Level 12

Hello,

 

You'll have to give more details than that because your code works fine :

 

 

data Dataset_1;
    length Country Continent $10;
    input Country $ Continent $;
    cards;
China Asia
Japan Asia
Malaysia Asia
Germany Europe
Australia Oceania
;
run;

data Dataset_2;
    length Continent Profit_Margin $10;
    input Continent $ Profit_Margin $;
cards;
Asia 0.05
Europe 0.08
America 0.02
;
run;

proc sort data=Dataset_1; by Continent; run;
proc sort data=Dataset_2; by Continent; run;

data want;
merge Dataset_1 (IN=A) Dataset_2 (IN=B);
by Continent;
if A;
run;

 Can you give the complete log for the data step ?

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 978 views
  • 0 likes
  • 3 in conversation