Hello, I am trying to merge several files but the merging should satisfy a specific condition. This is how my situation is:
1. I have a main file which includes an ID number and a year indicating which survey wave a respondent participated in. See below.
2) Now, I have to add a salary variable to my main file. The salary variable however are in different files. Each file contains salary information for a specific year and a lot of the respondents have salary information for multiple years.
3) What I want to do is to merge the salary for the respondents BUT only if the salary year matches the wave year for that particular respondent.
I tried merging each of the salary files and dropped cases that were not merged in the using file (see code below)
This code however only retained the salary information from the 2019 files since all the respondents have 2019 salary files.
I wanted to do something like the one below but Im getting an invalid syntax saying "if" is not allowed. Any suggestions on what syntax to use since the "if" condition is the only one I cant think of but obviously is not possible. Thank you in advance.
1. I have a main file which includes an ID number and a year indicating which survey wave a respondent participated in. See below.
Code:
* Example generated by -dataex-. For more info, type help dataex clear ID wave Salary 1 2011 . 2 2012 . 3 2010 . 4 2011 . 5 2010 . 6 2015 . 7 2019 . 8 2003 . 9 2003 . 10 2005 . end
3) What I want to do is to merge the salary for the respondents BUT only if the salary year matches the wave year for that particular respondent.
I tried merging each of the salary files and dropped cases that were not merged in the using file (see code below)
Code:
use "C:\Users\ASUS\OneDrive - mainfile.dta" merge ID using "C:\Users\ASUS\OneDrive - 2019_salary_data.dta", gen(merge2019) drop if merge2010==2 merge ID using "C:\Users\ASUS\OneDrive - 2015_salary_data.dta", gen(merge2015) drop if merge2015==2 merge ID using "C:\Users\ASUS\OneDrive - 2013_salary_data.dta", gen(merge2013) drop if merge2013==2
I wanted to do something like the one below but Im getting an invalid syntax saying "if" is not allowed. Any suggestions on what syntax to use since the "if" condition is the only one I cant think of but obviously is not possible. Thank you in advance.
Code:
use "C:\Users\ASUS\OneDrive - mainfile.dta" merge ID using "C:\Users\ASUS\OneDrive - 2019_salary_data.dta" if wave==2019

Comment