## How to pick alternative values form two Tables

Solved
Occasional Contributor
Posts: 11

# How to pick alternative values form two Tables

I have 5 names in table1 and 5 names in table2..

 Table1 Table2 Table A F A B G F C H B D I G E J C H D I E J

want to create a table with output picking values alternatively from two tables

Accepted Solutions
Solution
‎08-01-2017 02:21 AM
Super User
Posts: 2,046

## Re: How to pick alternative values form two Tables

A one to one merge?

data table1;

input var1 \$;

datalines;

A

B

C

D

E

;

data table2;

input var2 \$;

datalines;

F

G

H

I

J

;

data table;

set table1 ;

set table2;

var=var1;

output;

var=var2;

output;

drop var1 var2;

run;

All Replies
Posts: 1,848

## Re: How to pick alternative values form two Tables

For table1 id should be: 1 3 5 ...

For table2 id should be 2 4 6 ...

then merge the two tables and drop the temporary id.

``````data t_table1;
set table1;
retain id 1;
output;
id+2;
run;

data t_table2;
set table2;
retain id 2;
output;
id+2;
run;

data table;
merge t_table1 t_tabl2;
by id;
drop id;
run;``````
Solution
‎08-01-2017 02:21 AM
Super User
Posts: 2,046

## Re: How to pick alternative values form two Tables

A one to one merge?

data table1;

input var1 \$;

datalines;

A

B

C

D

E

;

data table2;

input var2 \$;

datalines;

F

G

H

I

J

;

data table;

set table1 ;

set table2;

var=var1;

output;

var=var2;

output;

drop var1 var2;

run;

Occasional Contributor
Posts: 11

## Re: How to pick alternative values form two Tables

can you explain me the logic as i dint get it

Super User
Posts: 2,046

## Re: How to pick alternative values form two Tables

The logic: Since you mentioned two tables, I created two sample tables each containing a variable as you may have noticed var1 in table1 and var2 in table2.

Typically, the entire SAS programming revolves around the following major concepts:

1. Bring the record into PDV(memory) area where you process

2. Reset values in PDV by editing, copying or computing any values of any variables. So the new variable var takes the value of var1

3. Before resetting again, make sure to use an explicit output so that you don't miss out on the requirement to have the alternative values

4. reset the value of var again this time with value if var2

5. Use another explicit output statement to get the values written to the dataset.

Important concepts in SAS:

1. Implicit pass/output and return

2. Explicit output overriding implicit output still implicit return

3. Zipping through the dataset in memory with a loop(do until eof)

4. Zipping through by groups in memory with a loop(do until(last.by_variable)

5. Look above and Look ahead. (Lag, retain, merge with firstobs=2 and so on)

6. Look up the entire table, column for multiple values-- easy way hashes

7. Familiarise with functions however i don't emphasise this as I believe the above 6 will lead you look for functions or even create your own

Hope that helps

Occasional Contributor
Posts: 11

## Re: How to pick alternative values form two Tables

Occasional Contributor
Posts: 11

## Re: How to pick alternative values form two Tables

output is not comming in single coloumn

its commig in two different coloumns like this

 name1 name2 A F B G C H D I E J

Super User
Posts: 2,046

## Re: How to pick alternative values form two Tables

That's the reason I reset the values to var instead of var1 and var2 in my example above.

var=var1;

output;

var=var2;

output;

This makes sure the output is in single column var  Please run my eample, you will get it

☑ This topic is solved.