Project

General

Profile

Support #907

Error message (invalid 'and') while trying to loop

Added by OLAYIWOLA OLADIRAN about 6 years ago. Updated about 6 years ago.

Status:
Resolved
Priority:
Urgent
Category:
-
Start date:
01/29/2018
% Done:

100%


Description

Hi,

I am trying to merge BHPS to the UKLS data. Using the command below, by the time I get to foreach (looping), it gives me an error message: _""""""invalid 'and'
r(198);"""""
What am I doing wrong?

capture mkdir M:\example9data
global dir3 "M:\example9data\"
log using $dir3\Example9.log, replace
foreach w in a b c d e f g h i j {
use pid `w'hid `w'pno `w'hlghq1 `w'jbstat `w'xrwght `w'sex ///
using $dir2/`w'indresp, clear
renpfix `w'
gen wave = strpos("abcdefghijklmnopqr","`w'")
recode jbstat (5=6) (6=7) (7=8) (8=5) if wave==1
// The coding frame of ajbstat needs to be aligned with that of other
// waves. Best to do this before appending other waves as labels get
// overwritten.
lab var wave "wave of BHPS interview"
sort pid wave
save $dir3/ind_junk`w', replace
}
foreach w in k l m n o p q r {
use pid `w'hid `w'pno `w'hlghq1 `w'jbstat `w'xrwght `w'xrwtuk1 `w'sex ///
using $dir2/`w'indresp, clear
renpfix `w'
capture rename id pid
// this line of command is only needed in wave 16 (p) so was not needed
// in the previous loop
gen wave = strpos("abcdefghijklmnopqr","`w'")
lab var wave "wave of BHPS interview"
sort pid wave
save $dir3/ind_junk`w', replace
}


Files

merging error.JPG (42.8 KB) merging error.JPG OLAYIWOLA OLADIRAN, 01/30/2018 01:57 PM
#1

Updated by Alita Nandi about 6 years ago

  • Status changed from New to In Progress
  • Assignee set to OLAYIWOLA OLADIRAN
  • % Done changed from 0 to 50
  • Private changed from Yes to No

If you have copied this code from the worksheets, then due to formatting issues, the ` which you need to specify the local macro item, generally does not work. So, please input this by hand in the do file directly. See if that takes care of the problem.

Also, please note that the latest Understanding Society data release includes harmonised UKHLS-BHPS files. The BHPS and UKHLS data files have not been merged but where the variables are harmonizable they have the same name. The user guide is available here:
https://www.understandingsociety.ac.uk/d/351/bhps-harmonised-user-guide.pdf?1511447890

#2

Updated by OLAYIWOLA OLADIRAN about 6 years ago

Thank you for your reply.
I am acutally using the do file you gave us at the training in Essex in November (Example 9); I have tried to change the ` but it is still giving me the same error message.

Alita Nandi wrote:

If you have copied this code from the worksheets, then due to formatting issues, the ` which you need to specify the local macro item, generally does not work. So, please input this by hand in the do file directly. See if that takes care of the problem.

Also, please note that the latest Understanding Society data release includes harmonised UKHLS-BHPS files. The BHPS and UKHLS data files have not been merged but where the variables are harmonizable they have the same name. The user guide is available here:
https://www.understandingsociety.ac.uk/d/351/bhps-harmonised-user-guide.pdf?1511447890

#3

Updated by OLAYIWOLA OLADIRAN about 6 years ago

That is the message I am getting (attached)

OLAYIWOLA OLADIRAN wrote:

Thank you for your reply.
I am acutally using the do file you gave us at the training in Essex in November (Example 9); I have tried to change the ` but it is still giving me the same error message.

Alita Nandi wrote:

If you have copied this code from the worksheets, then due to formatting issues, the ` which you need to specify the local macro item, generally does not work. So, please input this by hand in the do file directly. See if that takes care of the problem.

Also, please note that the latest Understanding Society data release includes harmonised UKHLS-BHPS files. The BHPS and UKHLS data files have not been merged but where the variables are harmonizable they have the same name. The user guide is available here:
https://www.understandingsociety.ac.uk/d/351/bhps-harmonised-user-guide.pdf?1511447890

#4

Updated by Alita Nandi about 6 years ago

Hello, I just ran the following code and did not get an error message. How did you specify the globals dir2 and dir3? If there are spaces in the filepaths dir2 and dir3 then you need to use double quotes where you specify these globals, e.g., "$dir2/`w'indresp"

global dir2 folderwhereihavestoredbhpsfiles
global dir3 folderwhereihavestoredoutputfiles

foreach w in a b c d e f g h i j {
use pid `w'hid `w'pno `w'hlghq1 `w'jbstat `w'xrwght `w'sex ///
using $dir2/`w'indresp, clear
renpfix `w'
gen wave = strpos("abcdefghijklmnopqr","`w'")
recode jbstat (5=6) (6=7) (7=8) (8=5) if wave==1
// The coding frame of ajbstat needs to be aligned with that of other
// waves. Best to do this before appending other waves as labels get
// overwritten.
lab var wave "wave of BHPS interview"
sort pid wave
save $dir3/ind_junk`w', replace
}
foreach w in k l m n o p q r {
use pid `w'hid `w'pno `w'hlghq1 `w'jbstat `w'xrwght `w'xrwtuk1 `w'sex ///
using $dir2/`w'indresp, clear
renpfix `w'
capture rename id pid
// this line of command is only needed in wave 16 (p) so was not needed
// in the previous loop
gen wave = strpos("abcdefghijklmnopqr","`w'")
lab var wave "wave of BHPS interview"
sort pid wave
save $dir3/ind_junk`w', replace
}

#5

Updated by OLAYIWOLA OLADIRAN about 6 years ago

Thank you Altida,
The problem was with me. The dofile I was using (the one we received from the workshop) had the variables and the BHPS waves with a different convention (a,b,c) from the harmonised dataset (which has ba, bb, ....), hence I needed to change these in the do file and include double "w" where necessary.
It took me about 3 days to figure out what I was doing wrong.
Thank you.

Alita Nandi wrote:

Hello, I just ran the following code and did not get an error message. How did you specify the globals dir2 and dir3? If there are spaces in the filepaths dir2 and dir3 then you need to use double quotes where you specify these globals, e.g., "$dir2/`w'indresp"

global dir2 folderwhereihavestoredbhpsfiles
global dir3 folderwhereihavestoredoutputfiles

foreach w in a b c d e f g h i j {
use pid `w'hid `w'pno `w'hlghq1 `w'jbstat `w'xrwght `w'sex ///
using $dir2/`w'indresp, clear
renpfix `w'
gen wave = strpos("abcdefghijklmnopqr","`w'")
recode jbstat (5=6) (6=7) (7=8) (8=5) if wave==1
// The coding frame of ajbstat needs to be aligned with that of other
// waves. Best to do this before appending other waves as labels get
// overwritten.
lab var wave "wave of BHPS interview"
sort pid wave
save $dir3/ind_junk`w', replace
}
foreach w in k l m n o p q r {
use pid `w'hid `w'pno `w'hlghq1 `w'jbstat `w'xrwght `w'xrwtuk1 `w'sex ///
using $dir2/`w'indresp, clear
renpfix `w'
capture rename id pid
// this line of command is only needed in wave 16 (p) so was not needed
// in the previous loop
gen wave = strpos("abcdefghijklmnopqr","`w'")
lab var wave "wave of BHPS interview"
sort pid wave
save $dir3/ind_junk`w', replace
}

#6

Updated by Alita Nandi about 6 years ago

  • % Done changed from 50 to 100

No worries! These things happen... Glad that the issue has been resolved.
Best wishes,
Alita

#7

Updated by Alita Nandi about 6 years ago

  • Status changed from In Progress to Resolved

Also available in: Atom PDF