ODKmeta error on Stata "invalid stub varname"

Hi ODK Community,@Matthew_White
I seek some help in troubleshooting my odkmeta do file.
I managed to generate my do file using the odkmeta command on Stata and then run the do file until I get the error "invalid stub services". Services is one of the variables I have in my data and is multiple select. I divided the variable into 3, services, services1 and services2. I understand this error comes due to the split command. I'm desperately looking for a solution for this.

I am using ODK Aggregate v1.4.15 windows-installer on 64bit windows 7 machine.

Dear Joyce,

I am wondering what you mean by "I divided the variable into 3" here and whether that might be part of the problem. odkmeta itself does the split, so if you have already split it will become confused. The relevant syntax in the .ado file that generates this error is:

  • initial check that generate() is valid name (will be used as stub)
    if "generate'"' != "" {
    * check it is a valid name
    confirm name generate' * only 1 word allowed if : word count generate'' > 1 { di as err "invalid stub generate'"
    exit 198
    }

and there is a note in the help file that
" 1. The do-file uses split to split select_multiple variables. split will result in
an error if a select_multiple variable has a long name or if splitting it would
result in duplicate variable names.
"
So I am initially guessing there is an issue here with "services" being repeated. Hope that helps!

Best,
Caroline

1 Like

Hi Caroline,
Thanks for your response. So during the programming of the questionnaire, I decided to come up with 3 different variable names which are services, services1, and services2. This is how they originally are in the downloaded csv data file.
I hope this now clarifies my question.

1 Like

Caroline is right that the issue has to do with duplicate variable names. Once services is split, the resulting variables will be services1 services2 services3 ..., but that conflicts with the existing variables services1 services2.

Rather than guessing at how to solve this conflict, odkmeta asks you to rename one of these variables. For example, you could rename services services1 services2 to services1 services2 services3.

It's important that you do this renaming in the right place. (For example, renaming before the char commands will likely result in an error.) The section of the do-file for splitting is preceded by a designated area for renaming. You should find that section of the do-file and complete the renaming there. I also recommend making sure you've read "Remarks for field names" in the help file.

Hope that helps!

2 Likes

Thanks, Matthew_White
I managed to solve the error by renaming the variables not only on my existing CSV data file but also on the do-file and the it was able to run further down till I encountered another error that I am trying to troubleshoot. If I don't manage to fix this new error, I hope I will be allowed to post it too.
Thanks again

1 Like

I'd actually recommend against renaming the column in the .csv file if possible. In general, I think it's best to minimize any manual editing of your raw .csv data, instead doing any data cleaning in your do-file. That prevents any editing errors and also makes your data cleaning more easily reproducible. Editing raw data can also have unforeseen side effects. For example, just the act of opening a .csv file in Excel, making a small change, and saving the file can reformat its date/time values.

Rather than editing the .csv and changing the char code in the do-file, I'd recommend using the designated area in the do-file for renaming. If you try that, let us know if you encounter any issues in doing so.

If you're not able to solve the new error yourself, you're certainly welcome to post it on the forum. I recommend using this same thread; if the error ends up being unrelated, we might ask a moderator to split it into a separate thread.

1 Like

Thanks for the advice, Matthew_White. I will refrain from making any changes in my raw data file for now and for future purposes. The new error I have encountered is the following:
invalid syntax
r(198)
I have explored the do-file and realized that this error happens just before the "merge repeat groups section". Is there any way around this?

1 Like

It can be a little tricky to troubleshoot with that error message alone. Can you tell which command issued the error message? If the command is within a for-loop, it might not be easy to tell: type set trace on in Stata to get more details about which commands are being run. What are the last several commands that preceded the error message?

1 Like
  • version 10.0
    • gettoken val : 0
    • if (strpos("val'", "val") > 0 ) { = if (strpos("drop", "val") > 0 ) { gettoken val 0 : 0 syntax anything [, nofix] if "fix'" != "" {
      local fix ", nofix"
      }
      gettoken var rest : anything
      while "rest'"' != "" {
      gettoken lab rest : rest
      local label "lab'" } local vlist : list anything - lab if "lab'" == "." {
      local lab ""
      }
      foreach var of varlist vlist' { _label val' var' lab' `fix'
      }
      }
    • else {
    • _label `macval(0)'
      = _label drop _all
    • }
      --------------------------------------------------------------------------------------------------------------------------- end label ---
  • }
  • else if "`1'"=="mata" {
    = else if ""=="mata" {
    mata: mata clear
    }
  • else if inlist("1'", "results", "matrix") { = else if inlist("", "results", "matrix") { return clear clearreturn ereturn clear sreturn clear _return drop _all if ("1'" == "matrix") {
    matrix drop _all
    _est drop _all
    }
    }
  • else if "`1'"=="programs" {
    = else if ""=="programs" {
    program drop _all
    }
  • else if "`1'"=="ado" {
    = else if ""=="ado" {
    program drop _allado
    }
  • else if "1'"=="*" | "1'"=="all" {
    = else if ""=="*" | ""=="all" {
    capture mata: st_local("semmods", strofreal(sg__global.hasmodels()))
    capture
    if (0`semmods') {
    display as err "-clear all- not allowed while an SEM Builder is open"
    exit 1
    }
    drop _all
    label drop _all
    matrix drop _all
    scalar drop _all
    constraint drop _all
    eq drop _all
    file close _all
    postutil clear
    _return drop _all
    discard
    program drop _all
    timer clear
    mata: mata clear
    }
  • else {
    display as err "`1' not allowed"
    exit 198
    }

I hope I have managed to copy all the necessary data required to determine the commands being run from that. The last commands that come before the error message are:

local allbadnames "allbadnames' "badnames'""' ds, not(char Odk_name) local datanotform r(varlist)'
local exclude SubmissionDate KEY PARENT_KEY metainstanceID
local datanotform : list datanotform - exclude
local alldatanotform "alldatanotform' "`datanotform'""'
save, replace
}

1 Like

(I accidentally posted that last reply too early...)

I think those last commands you mention (starting with local allbadnames ...) are just the last commands of the for-loop. It seems like the command issuing the error message is somewhere in the for-loop, but it could be anywhere in the for-loop.

Unfortunately, although set trace on is a useful way to get more information about what commands are being run, it can also provide too much information. It shows not just what commands the do-file is running, but also what commands are being run as part of those commands, what commands are then being run as part of those commands, and so on.

As far as I can tell, the trace you've copied is from the clear command, which is unlikely to be the source of the error. We need to know the last line of the do-file that Stata tried to run, not just the commands that were run as part of that line.

As part of investigating that, you may wish to use other trace-related settings: type help trace in Stata for more information. For example, I recommend set tracedepth 1 for now, which will reduce the level of detail in the trace.

For more information, I recommend visiting Statalist, which is the official Stata forum. There you should be able to search for discussions on how to debug a do-file and a for-loop.

Once we know which exact line of the do-file is issuing the error message, we should be able to make more progress.

1 Like

Here are a few other resources on debugging in Stata:

3 Likes

I went through the debugging Stata resources and learned quite a lot. Thanks Matthew. Well today when I resumed on working on my do-file, to use the "set trace on" command, I experienced a totally different error from the one I previously had. The error is as follows:
Unexpected values encountered:
county_hospital
dispensary
mobile_clinic
private_hospital
sub_county_hospital
r(459);
Those are some of the choices/responses for one of the variables "services" which I had renamed earlier to allow the split command to work. How can I fix this?

This isn't an issue I've seen before with the odkmeta do-file, but that looks like an error from the encode command. encode can issue that error message if it encounters a value in the variable that is not present in the value label it is using to encode. In this case, encode would issue that error message if the value label it is using to encode the services variables does not include county_hospital, dispensary, etc.

One common pattern for ODK choice lists is to have names that are integers and labels that are string. Another pattern is names and labels that are both string. An example of that second pattern would be a choice whose name is private_hospital and whose label is "Private hospital".

Stata value labels usually associate integer values with string labels, making them an easier fit for the first pattern than the second. For the second pattern, the odkmeta do-file first replaces the string names of the ODK list with their associated string labels before running encode. In that case, you would see commands like the following:

replace `temp' = "Private hospital" if `var' == "private_hospital"

If for some reason those replace commands aren't successful, encode might fail. I recommend doing some debugging around those replace commands to make sure that they are running and that they are successfully completing the replacements.

For more information, I recommend reading "Remarks for lists" in the odkmeta help file. The Stata documentation on value labels and the encode command are also quite helpful.

1 Like

I followed the instructions on "Remarks for lists" in the odkmeta help file and I also corrected a mistake I had made on my do-file accidentally which enabled me to successfully solve the error. I however encountered a similar error as before, the only difference is that the choice list is for a repeat group.
Unexpected values encountered:
2013
2014
2015
2016
2017
r(459);
Those are the years in which farmers experienced reduced crop yields. Could there be something I am missing? I'm assuming I have not made any mistakes while editing my do-file.

Dear Joyce,

This seems to be a common challenge as there are existing posts on the Forum on the issue. In this series of posts someone encountered the same error and solved it:

You can search for the other posts with the same error as well, hopefully they help!

Best,
Caroline

1 Like

Thanks Caroline.

I will check it out.