Quartz | Level 8

## Merging Variables

Well, I'm afraid I'm back with another easy question, but I am stuck.  Or rather, I haven't gotten this to work in a way that I am happy.

I have three datasets that have been successfully combined, no problems there. The problem is when I am looking at the data, a number of variables were changed over the years the data was gathered.

The question is, "how many of the last 12 months were you employed".

In 2007, this variable was titled Months (which was later used for something completely different, at least I figured out how to separate that mess).  Then in 2008 it was changed to PastPay, and then in 2010 it was changed again to EMP_PAY. The data is pretty straightforward, respondents answered 1-12.  So it would look like:

2007

Obs1 = 1

Obs2 = 3

Obs3 = 9

Obs3 = 5

Obs4 = 11

2008

Obs1 = 2

Obs2 = 3

Obs3 = 8

Obs3 = 12

Obs4 = 7

2010

Obs1 = 9

Obs2 = 2

Obs3 = 4

Obs3 = 1

Obs4 = 8

I've tried do loops with if/then statements, tried catx, and tried an array but I haven't been able to get them combined.  (largely because I'm still learning SAS).  Sorry, I'm sure I'll have 8 simple answers and feel really dumb here in about 10 minutes but I'm hitting a wall right now.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Merging Variables

COALESCE().

Assumptions:

• the variable is numeric
• you want them combined into a single variable (never actually stated what you wanted as output).

``employed_prev12 = coalesce(Months, PastPay, Emp_Pay);``

@DanielQuay wrote:

Well, I'm afraid I'm back with another easy question, but I am stuck.  Or rather, I haven't gotten this to work in a way that I am happy.

I have three datasets that have been successfully combined, no problems there. The problem is when I am looking at the data, a number of variables were changed over the years the data was gathered.

The question is, "how many of the last 12 months were you employed".

In 2007, this variable was titled Months (which was later used for something completely different, at least I figured out how to separate that mess).  Then in 2008 it was changed to PastPay, and then in 2010 it was changed again to EMP_PAY. The data is pretty straightforward, respondents answered 1-12.  So it would look like:

2007

Obs1 = 1

Obs2 = 3

Obs3 = 9

Obs3 = 5

Obs4 = 11

2008

Obs1 = 2

Obs2 = 3

Obs3 = 8

Obs3 = 12

Obs4 = 7

2010

Obs1 = 9

Obs2 = 2

Obs3 = 4

Obs3 = 1

Obs4 = 8

I've tried do loops with if/then statements, tried catx, and tried an array but I haven't been able to get them combined.  (largely because I'm still learning SAS).  Sorry, I'm sure I'll have 8 simple answers and feel really dumb here in about 10 minutes but I'm hitting a wall right now.

2 REPLIES 2
Super User

## Re: Merging Variables

COALESCE().

Assumptions:

• the variable is numeric
• you want them combined into a single variable (never actually stated what you wanted as output).

``employed_prev12 = coalesce(Months, PastPay, Emp_Pay);``

@DanielQuay wrote:

Well, I'm afraid I'm back with another easy question, but I am stuck.  Or rather, I haven't gotten this to work in a way that I am happy.

I have three datasets that have been successfully combined, no problems there. The problem is when I am looking at the data, a number of variables were changed over the years the data was gathered.

The question is, "how many of the last 12 months were you employed".

In 2007, this variable was titled Months (which was later used for something completely different, at least I figured out how to separate that mess).  Then in 2008 it was changed to PastPay, and then in 2010 it was changed again to EMP_PAY. The data is pretty straightforward, respondents answered 1-12.  So it would look like:

2007

Obs1 = 1

Obs2 = 3

Obs3 = 9

Obs3 = 5

Obs4 = 11

2008

Obs1 = 2

Obs2 = 3

Obs3 = 8

Obs3 = 12

Obs4 = 7

2010

Obs1 = 9

Obs2 = 2

Obs3 = 4

Obs3 = 1

Obs4 = 8

I've tried do loops with if/then statements, tried catx, and tried an array but I haven't been able to get them combined.  (largely because I'm still learning SAS).  Sorry, I'm sure I'll have 8 simple answers and feel really dumb here in about 10 minutes but I'm hitting a wall right now.

Quartz | Level 8

## Re: Merging Variables

Thank You again Reeza,

And I'm sorry, I promise, I'm trying to learn how to ask questions better.

Discussion stats
• 2 replies
• 588 views
• 3 likes
• 2 in conversation