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

Hi friends, it's difficult to elaborate.

 

Let's say for Table A, I have column 'firmname' and 'dealyear' representing when a M&A deal was taken place.

and for Table B I have column 'firmname', '2015', '2016', '2017', '2018', and '2019', representing annual average stock value of the company.

 

Now I wanna add column called 'stockprice' in table A, thus, for the respective deal info in table A there would be a stock value of the company, of the year.

 

Can anyone let me know how to achieve this?

 

example of table A:

Company_Namedealyearstockprice ( to be added with the value from Table B)
Abcam plc2000 
Abeona Therapeutics Inc1999 
Abiomed Inc2001 
Acadia Healthcare2002 

 

example of table B:

Company_Name_1999_2000_2001_2002
Abcam plc6.6487487.52630514.2162117.03757
Abeona Therapeutics Inc602.33581363.622933.9541667.004
Abiomed Inc8.45799224.7208620.061187.379444
Acadia Healthcare4.0927054.4838081.4579232.454762
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Transpose your B table from wide to long and look up from there. Makes the whole problem much simpler 🙂

 

data a;
input Company_Name:$50. dealyear;
infile datalines dlm=',';
datalines;
Abcam plc,2000
Abeona Therapeutics Inc,1999
Abiomed Inc,2001
Acadia Healthcare,2002
;

data b;
input Company_Name:$50. _1999 _2000 _2001 _2002;
infile datalines dlm=',';
datalines;
Abcam plc,6.648748,7.526305,14.21621,17.03757
Abeona Therapeutics Inc,602.3358,1363.622,933.9541,667.004
Abiomed Inc,8.457992,24.72086,20.06118,7.379444
Acadia Healthcare,4.092705,4.483808,1.457923,2.454762
;

/* transpose b from wide to long */
data b_long(keep=Company_Name dealyear stockprice);
  set b;
  array y{*} _1999-_2002;
  do i=1 to dim(y);
     dealyear=input(compress(vname(y[i]), '_'), best.);
     stockprice=y[i];
     output;
  end;
run;

proc sql;
   create table want as
   select a.*,
          b_long.stockprice
   from a, b_long
   where a.Company_Name=b_long.Company_Name
   and   a.dealyear=b_long.dealyear;
quit;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Transpose your B table from wide to long and look up from there. Makes the whole problem much simpler 🙂

 

data a;
input Company_Name:$50. dealyear;
infile datalines dlm=',';
datalines;
Abcam plc,2000
Abeona Therapeutics Inc,1999
Abiomed Inc,2001
Acadia Healthcare,2002
;

data b;
input Company_Name:$50. _1999 _2000 _2001 _2002;
infile datalines dlm=',';
datalines;
Abcam plc,6.648748,7.526305,14.21621,17.03757
Abeona Therapeutics Inc,602.3358,1363.622,933.9541,667.004
Abiomed Inc,8.457992,24.72086,20.06118,7.379444
Acadia Healthcare,4.092705,4.483808,1.457923,2.454762
;

/* transpose b from wide to long */
data b_long(keep=Company_Name dealyear stockprice);
  set b;
  array y{*} _1999-_2002;
  do i=1 to dim(y);
     dealyear=input(compress(vname(y[i]), '_'), best.);
     stockprice=y[i];
     output;
  end;
run;

proc sql;
   create table want as
   select a.*,
          b_long.stockprice
   from a, b_long
   where a.Company_Name=b_long.Company_Name
   and   a.dealyear=b_long.dealyear;
quit;
jimmychoi
Obsidian | Level 7
Hi Draycut, your code works just perfect. and for the transposing part, can I do similar thing using SQL?
PeterClemmensen
Tourmaline | Level 20

Also, if you prefer a data step lookup approach, replace the PROC SQL step with 

 

data want;
   if 0 then set b_long;
   if _N_ = 1 then do;
      declare hash h(dataset:'b_long');
      h.defineKey('Company_Name', 'dealyear');
      h.defineData('stockprice');
      h.defineDone();
   end;

   set a;

   rc=h.find();
run;
FreelanceReinh
Jade | Level 19

Or simply merge by Company_Name:

data have_a;
input Company_Name $25. dealyear;
cards;
Abcam plc                2000  
Abeona Therapeutics Inc  1999  
Abiomed Inc              2001  
Acadia Healthcare        2002  
;

data have_b;
input Company_Name $25. _1999-_2002;
cards;
Abcam plc                6.648748 7.526305 14.21621 17.03757
Abeona Therapeutics Inc  602.3358 1363.622 933.9541 667.004
Abiomed Inc              8.457992 24.72086 20.06118 7.379444
Acadia Healthcare        4.092705 4.483808 1.457923 2.454762
;

data want(drop=_:);
merge have_a(in=a) have_b;
by Company_Name;
if a;
array _[1999:2002] _:;
stockprice=_[dealyear];
run;

Alternatively, you can omit the ARRAY statement and define stockprice by means of the VVALUEX function:

stockprice=input(vvaluex(cats('_',dealyear)),12.);

Of course, merging by Company_Name requires that there are no spelling differences (upper/lower case, puctuation, etc.) between the two datasets and that they are sorted by this variable.

novinosrin
Tourmaline | Level 20

Hi @jimmychoi   Your table B names with _ prefix gives me the impression, that's actually  result of a transposed dataset?

 

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
  • 5 replies
  • 1449 views
  • 2 likes
  • 4 in conversation