DATA Step, Macro, Functions and more

Creating a new variable with information from 2 tables

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Creating a new variable with information from 2 tables

Hello,
I would like to add a column to Table 1, the "des" column.

 

If table1.cat = table2.id then table1.des = table2.des

 

 

Table1:

CatName
1zer
4tez
3dgr
1ffz
1fzgtjt
2pojn
2fj
3kfi
3ndonz

 

Table 2

 

iddes
1com
2the
3action
4sade

 

And the table that I receive

 

CatNamedes
1zercom
4tezsade
3dgraction
1ffzcom
1fzgtjtcom
2pojnthe
2fjthe
3kfiaction
3ndonzaction

 

Thanks for your help


Accepted Solutions
Solution
‎02-24-2018 09:48 AM
PROC Star
Posts: 1,209

Re: Creating a new variable with information from 2 tables

Do like this

 

data table1;
input Cat Name $10.;
datalines;
1 zer
4 tez
3 dgr
1 ffz
1 fzgtjt
2 pojn
2 fj
3 kfi
3 ndonz
;

data table2;
input id	des $10.;
datalines;
1 com
2 the
3 action
4 sade
;

proc sql;
   create table want as
   select table1.*
         ,table2.des
   from table1, table2
   where table1.cat=table2.id;
quit;

proc print data=want;run;

View solution in original post


All Replies
Solution
‎02-24-2018 09:48 AM
PROC Star
Posts: 1,209

Re: Creating a new variable with information from 2 tables

Do like this

 

data table1;
input Cat Name $10.;
datalines;
1 zer
4 tez
3 dgr
1 ffz
1 fzgtjt
2 pojn
2 fj
3 kfi
3 ndonz
;

data table2;
input id	des $10.;
datalines;
1 com
2 the
3 action
4 sade
;

proc sql;
   create table want as
   select table1.*
         ,table2.des
   from table1, table2
   where table1.cat=table2.id;
quit;

proc print data=want;run;
Contributor
Posts: 33

Re: Creating a new variable with information from 2 tables

Great thank you very much!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 135 views
  • 0 likes
  • 2 in conversation