DATA Step, Macro, Functions and more

How to pick alternative values form two Tables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

How to pick alternative values form two Tables

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

Table1Table2Table
AFA
BGF
CHB
DIG
EJC
  H
  D
  I
  E
  J

 

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


Accepted Solutions
Solution
3 weeks ago
PROC Star
Posts: 169

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;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,374

Re: How to pick alternative values form two Tables

Add a temporary id:

    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
3 weeks ago
PROC Star
Posts: 169

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: 8

Re: How to pick alternative values form two Tables

Thank you for your reply @novinosrin .... its working....

 

can you explain me the logic as i dint get it

PROC Star
Posts: 169

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: 8

Re: How to pick alternative values form two Tables

Thankyou for your explanation @novinosrin

Occasional Contributor
Posts: 8

Re: How to pick alternative values form two Tables

output is not comming in single coloumn

its commig in two different coloumns like this

 

name1name2
A 
 F
B 
 G
C 
 H
D 
 I
E 
 J

 

 

PROC Star
Posts: 169

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.

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

Discussion stats
  • 7 replies
  • 211 views
  • 2 likes
  • 3 in conversation