Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error reshape wide to long: "variable 0_welsh implied name too long"

    Hello,

    I used the following command to reshape data from wide to long (example of the data below).

    Code:
    ds cg_* ce_* local varlist = ustrregexra("`r(varlist)'","^(cg|ce)_","@_") reshape long `varlist', i(pidp) j(_j) string replace _j = cond(_j=="ce","September 2020","January 2021")

    However, when I run this command I get the following error: "variable 0_welsh implied name too long". I tried to delete this variable (because it was not relevanr) but when I run the command again appears the same error with other variables. Can you help me to solve this?
    Thank you in advance.

    Example of the data:

    * Example generated by -dataex-. For more info, type help dataex clear input long(pidp pid) int i_strata byte(ce_semp cg_semp cg_parent5plus ce_parent5plus cg_couple ce_couple cg_sex_cv ce_sex_cv i_qfhigh) 76165 10689869 6 1 -8 2 2 1 1 2 2 -8 280165 12430439 15 -8 . . 1 . 1 . 2 -8 469205 13857142 25 -8 . . 2 . 2 . 2 15 732365 15752658 43 -8 -8 2 2 2 2 1 1 -8 1587125 17870879 65 3 -8 2 2 2 2 2 2 -8 4849085 176725733 148 1 -8 2 2 1 1 1 1 -8 68002725 10023526 1 4 -8 2 2 2 2 2 2 -8 68008847 -8 2006 1 -8 2 2 2 2 2 2 -8 68010887 -8 2006 1 -8 2 2 1 1 2 2 -8 68031967 -8 2030 4 4 2 2 2 2 2 2 -8 68035365 10403086 4 4 -8 2 2 2 2 1 1 -8 68035367 -8 2030 1 -8 2 2 1 1 1 1 -8 68041487 -8 2042 1 -8 1 1 1 1 2 2 -8 68045567 -8 2042 1 -8 2 2 2 2 2 2 -8 68051007 -8 2054 2 -8 2 2 1 1 1 1 -8 68051011 -8 2054 1 -8 2 2 1 1 2 2 -8 68058487 -8 2054 4 -8 2 2 1 1 1 1 -8 68058491 -8 2054 4 -8 2 2 1 1 2 2 -8 68060531 -8 2054 1 -8 1 1 1 1 2 2 -8 68060533 160066204 6 4 -8 2 2 1 1 2 2 -8 68060537 160066239 6 4 -8 2 2 1 1 1 1 -8 68061288 -8 2006 -8 . . 2 . 1 . 2 -8 68063247 -8 2066 1 -8 1 1 1 1 2 2 -8 68063927 -8 2066 1 -8 1 1 1 1 2 2 -8 68063931 -8 2066 -8 -8 1 1 1 1 1 1 -8 68064605 10653872 6 4 -8 2 2 1 1 1 1 -8 68064609 10653902 6 4 -8 2 2 1 1 2 2 -8 68068007 -8 2066 1 -8 2 2 1 1 1 1 -8 68068011 -8 2066 1 . . 2 . 2 . 2 -8 68068082 -8 2006 2 -8 2 2 1 1 1 1 -8 68097245 10913629 8 4 -8 2 2 2 2 2 2 -8 68097927 -8 2090 4 -8 2 2 2 2 2 2 -8 68112211 -8 2114 1 . . 1 . 1 . 2 -8 68120367 -8 2114 4 -8 2 2 2 2 2 2 -8 68120375 -8 2114 1 -8 1 2 2 2 2 2 -8 68125127 -8 2126 1 -8 1 1 1 1 2 2 -8 68125131 -8 2126 1 -8 2 2 1 1 1 1 -8 68125135 -8 2126 1 -8 2 2 2 2 2 2 -8 68133285 11218193 11 4 -8 2 2 2 2 2 2 -8 68133289 11218282 11 4 -8 2 2 1 1 2 2 -8 68136009 11234989 11 1 -8 1 1 2 2 2 2 -8 68137365 11240547 11 -8 -8 2 2 2 2 2 2 -8 68138045 11242787 11 4 -8 2 2 1 1 1 1 -8 68138049 11242817 11 4 -8 2 2 1 1 2 2 -8 68138051 -8 2138 4 -8 2 2 1 1 2 2 -8 68144847 -8 2138 1 -8 2 2 1 1 1 1 -8 68144851 -8 2138 1 -8 1 1 1 1 2 2 -8 68148247 -8 2150 -8 -8 2 2 1 1 1 1 -8 68148251 -8 2150 4 . . 2 . 1 . 2 -8 68150967 -8 2150 1 . . 2 . 1 . 1 -8 68150971 -8 2150 1 -8 2 2 1 1 2 2 -8 68150975 -8 2150 1 -8 2 2 2 2 1 1 -8 68155047 -8 2150 -8 4 1 1 1 1 2 2 -8 68155051 -8 2150 1 4 1 1 1 1 1 1 -8 68157771 -8 2150 -8 2 2 2 2 2 2 2 -8 68159131 -8 2150 1 -8 1 1 1 1 2 2 -8 68160485 11418567 11 1 -8 1 1 2 2 2 2 -8 68160489 11418591 11 1 -8 2 2 2 2 1 1 -8 68173407 -8 2174 4 -8 2 2 1 2 2 2 -8 68174767 -8 2174 4 . . 1 . 1 . 2 -8 68180887 -8 2174 1 -8 1 1 1 1 2 2 -8 68180891 -8 2174 1 . . 1 . 1 . 1 -8 68185647 -8 2186 4 -8 2 2 2 2 2 2 -8 68187687 -8 2186 4 -8 1 1 1 1 1 1 -8 68187691 -8 2186 4 -8 1 1 1 1 2 2 -8 68191771 -8 2186 1 -8 1 1 2 2 2 2 -8 68193127 -8 2186 4 -8 1 1 2 2 2 2 -8 68195167 -8 2186 4 -8 2 2 1 1 1 1 -8 68195171 -8 2186 4 -8 2 2 1 1 2 2 -8 68195851 -8 2186 1 -8 2 2 1 1 2 2 -8 68197211 -8 2198 1 . . 2 . 1 . 2 -8 68197887 -8 2198 1 -8 1 1 2 2 2 2 -8 68197899 -8 2198 -8 -8 2 1 2 2 2 2 -8 68197903 -8 2198 -8 -8 2 2 2 2 1 1 -8 68199247 -8 2198 1 -8 2 2 1 1 1 1 -8 68207407 -8 2198 4 -8 2 2 1 1 2 2 -8 68207411 -8 2198 4 -8 2 2 1 1 1 1 -8 68211487 -8 2210 4 -8 2 2 2 2 1 1 -8 68214207 -8 2210 1 -8 2 2 2 2 1 1 -8 68214887 -8 2210 -8 . . 1 . 1 . 1 -8 68214891 -8 2210 1 . . 1 . 1 . 2 -8 68216247 -8 2210 1 -8 1 1 1 1 2 2 -8 68218287 -8 2210 -8 -8 2 2 2 2 1 1 -8 68230527 -8 2222 -8 . . 1 . 2 . 2 -8 68231223 -8 2222 4 -8 2 2 2 2 2 2 -8 68238011 -8 2234 1 -8 2 2 1 1 2 2 -8 68262487 -8 2258 2 -8 2 2 1 1 1 1 -8 68266567 -8 2258 4 -8 2 2 2 2 2 2 -8 68278127 -8 2270 4 -8 2 2 2 2 2 2 -8 68288327 -8 2282 1 -8 1 1 1 1 2 2 -8 68288331 -8 2282 1 -8 1 1 1 1 1 1 -8 68291731 -8 2282 4 -8 2 2 2 2 2 2 -8 68293087 -8 2282 4 -8 1 1 1 1 2 2 -8 68293091 -8 2282 1 -8 1 1 1 1 1 1 -8 68293095 -8 2282 1 -8 1 1 1 1 1 1 -8 68293099 -8 2282 1 -8 2 2 1 1 1 1 -8 68293168 -8 2054 4 -8 2 2 2 2 1 1 -8 68294447 -8 2294 4 4 2 2 1 1 1 1 -8 68294451 -8 2294 1 1 2 2 1 1 2 2 -8 68297845 12521361 15 4 -8 2 2 1 1 1 1 -8 end label values pid pid label def pid -8 "inapplicable", modify label values i_strata i_strata label values ce_semp ce_semp label def ce_semp -8 "inapplicable", modify label def ce_semp 1 "Yes, employed only", modify label def ce_semp 2 "Yes, self-employed only", modify label def ce_semp 3 "Both employed and self-employed", modify label def ce_semp 4 "No", modify label values cg_semp cg_semp label def cg_semp -8 "inapplicable", modify label def cg_semp 1 "Yes, employed only", modify label def cg_semp 2 "Yes, self-employed only", modify label def cg_semp 4 "No", modify label values cg_parent5plus cg_parent5plus label def cg_parent5plus 1 "Yes", modify label def cg_parent5plus 2 "No", modify label values ce_parent5plus ce_parent5plus label def ce_parent5plus 1 "Yes", modify label def ce_parent5plus 2 "No", modify label values cg_couple cg_couple label def cg_couple 1 "Yes", modify label def cg_couple 2 "No", modify label values ce_couple ce_couple label def ce_couple 1 "Yes", modify label def ce_couple 2 "No", modify label values cg_sex_cv cg_sex_cv label def cg_sex_cv 1 "Male", modify label def cg_sex_cv 2 "Female", modify label values ce_sex_cv ce_sex_cv label def ce_sex_cv 1 "Male", modify label def ce_sex_cv 2 "Female", modify label values i_qfhigh i_qfhigh label def i_qfhigh -8 "inapplicable", modify label def i_qfhigh 15 "Other school (inc. school leaving exam certificate or matriculation)", modify

  • #2
    Your -dataex- got mangled and it would take too long to unscramble it. But without looking at the data, I can tell you that the error message is, in this case, misleading. The problem is not that 0_welsh would be too long; the problem is that any name beginning with a 0 (or any other non-alphabetic character or underscore) would be illegal. That is why the problem keeps popping up when you remove a variable--the problem is generic. None of the renames you are attempting is legal. You have to put the _0 at the end of the variable name. Putting a digit at the start of a variable name is always illegal.

    So, I think you need to replace your pre--reshape commands with something like this:

    Code:
    ds cg_* ce_*
    local varlist `r(varlist)'
    local varlist: subinstr local varlist " " "_@ ", all
    local varlist `varlist'_@
    and then go from there with your same -reshape- command.

    Comment


    • #3
      Dear Clyde,
      I tried to run the command above and appeared a message saying that I had too may variables. I then run the command "set maxvar 32767, permanently" to tried to get more space. After that I run your command again but it didn´t work: It only apears data from january 2021. I don´t now what happened. Can you help me with this? Thank you in advance. I send an example of the data below:

      * Example generated by -dataex-. For more info, type help dataex
      clear
      input long(pidp psu) int strata byte(ce_semp cg_semp cg_parent5plus ce_parent5plus cg_couple ce_couple cg_sex_cv ce_sex_cv i_qfhigh)
      76165 19 6 1 -8 2 2 1 1 2 2 -8
      280165 67 15 -8 . . 1 . 1 . 2 -8
      469205 106 25 -8 . . 2 . 2 . 2 15
      732365 157 43 -8 -8 2 2 2 2 1 1 -8
      1587125 215 65 3 -8 2 2 2 2 2 2 -8
      4849085 560 148 1 -8 2 2 1 1 1 1 -8
      68002725 1 1 4 -8 2 2 2 2 2 2 -8
      68008847 2012 2006 1 -8 2 2 2 2 2 2 -8
      68010887 2012 2006 1 -8 2 2 1 1 2 2 -8
      68029931 2060 2030 . -8 1 . 1 . 1 . -8
      68031967 2060 2030 4 4 2 2 2 2 2 2 -8
      68035365 11 4 4 -8 2 2 2 2 1 1 -8
      68035367 2060 2030 1 -8 2 2 1 1 1 1 -8
      68041487 2084 2042 1 -8 1 1 1 1 2 2 -8
      68041491 2084 2042 . -8 1 . 1 . 1 . -8
      68045567 2084 2042 1 -8 2 2 2 2 2 2 -8
      68051007 2108 2054 2 -8 2 2 1 1 1 1 -8
      68051011 2108 2054 1 -8 2 2 1 1 2 2 -8
      68058487 2108 2054 4 -8 2 2 1 1 1 1 -8
      68058491 2108 2054 4 -8 2 2 1 1 2 2 -8
      68060531 2108 2054 1 -8 1 1 1 1 2 2 -8
      68060533 18 6 4 -8 2 2 1 1 2 2 -8
      68060537 18 6 4 -8 2 2 1 1 1 1 -8
      68061288 2012 2006 -8 . . 2 . 1 . 2 -8
      68063247 2132 2066 1 -8 1 1 1 1 2 2 -8
      68063927 2132 2066 1 -8 1 1 1 1 2 2 -8
      68063931 2132 2066 -8 -8 1 1 1 1 1 1 -8
      68064605 18 6 4 -8 2 2 1 1 1 1 -8
      68064609 18 6 4 -8 2 2 1 1 2 2 -8
      68068007 2132 2066 1 -8 2 2 1 1 1 1 -8
      68068011 2132 2066 1 . . 2 . 2 . 2 -8
      68068082 2012 2006 2 -8 2 2 1 1 1 1 -8
      68097245 25 8 4 -8 2 2 2 2 2 2 -8
      68097927 2180 2090 4 -8 2 2 2 2 2 2 -8
      68112211 2228 2114 1 . . 1 . 1 . 2 -8
      68120367 2228 2114 4 -8 2 2 2 2 2 2 -8
      68120375 2228 2114 1 -8 1 2 2 2 2 2 -8
      68125127 2252 2126 1 -8 1 1 1 1 2 2 -8
      68125131 2252 2126 1 -8 2 2 1 1 1 1 -8
      68125135 2252 2126 1 -8 2 2 2 2 2 2 -8
      68133285 34 11 4 -8 2 2 2 2 2 2 -8
      68133289 34 11 4 -8 2 2 1 1 2 2 -8
      68136009 34 11 1 -8 1 1 2 2 2 2 -8
      68137365 34 11 -8 -8 2 2 2 2 2 2 -8
      68138045 34 11 4 -8 2 2 1 1 1 1 -8
      68138049 34 11 4 -8 2 2 1 1 2 2 -8
      68138051 2276 2138 4 -8 2 2 1 1 2 2 -8
      68144847 2276 2138 1 -8 2 2 1 1 1 1 -8
      68144851 2276 2138 1 -8 1 1 1 1 2 2 -8
      68148247 2300 2150 -8 -8 2 2 1 1 1 1 -8
      68148251 2300 2150 4 . . 2 . 1 . 2 -8
      68150967 2300 2150 1 . . 2 . 1 . 1 -8
      68150971 2300 2150 1 -8 2 2 1 1 2 2 -8
      68150975 2300 2150 1 -8 2 2 2 2 1 1 -8
      68155047 2300 2150 -8 4 1 1 1 1 2 2 -8
      68155051 2300 2150 1 4 1 1 1 1 1 1 -8
      68157771 2300 2150 -8 2 2 2 2 2 2 2 -8
      68159131 2300 2150 1 -8 1 1 1 1 2 2 -8
      68160485 39 11 1 -8 1 1 2 2 2 2 -8
      68160489 39 11 1 -8 2 2 2 2 1 1 -8
      68173407 2348 2174 4 -8 2 2 1 2 2 2 -8
      68174767 2348 2174 4 . . 1 . 1 . 2 -8
      68180887 2348 2174 1 -8 1 1 1 1 2 2 -8
      68180891 2348 2174 1 . . 1 . 1 . 1 -8
      68184971 2372 2186 . -8 1 . 1 . 2 . -8
      68185647 2372 2186 4 -8 2 2 2 2 2 2 -8
      68187687 2372 2186 4 -8 1 1 1 1 1 1 -8
      68187691 2372 2186 4 -8 1 1 1 1 2 2 -8
      68191771 2372 2186 1 -8 1 1 2 2 2 2 -8
      68193127 2372 2186 4 -8 1 1 2 2 2 2 -8
      68195167 2372 2186 4 -8 2 2 1 1 1 1 -8
      68195171 2372 2186 4 -8 2 2 1 1 2 2 -8
      68195851 2372 2186 1 -8 2 2 1 1 2 2 -8
      68197211 2396 2198 1 . . 2 . 1 . 2 -8
      68197887 2396 2198 1 -8 1 1 2 2 2 2 -8
      68197899 2396 2198 -8 -8 2 1 2 2 2 2 -8
      68197903 2396 2198 -8 -8 2 2 2 2 1 1 -8
      68199247 2396 2198 1 -8 2 2 1 1 1 1 -8
      68207407 2396 2198 4 -8 2 2 1 1 2 2 -8
      68207411 2396 2198 4 -8 2 2 1 1 1 1 -8
      68211487 2420 2210 4 -8 2 2 2 2 1 1 -8
      68214207 2420 2210 1 -8 2 2 2 2 1 1 -8
      68214887 2420 2210 -8 . . 1 . 1 . 1 -8
      68214891 2420 2210 1 . . 1 . 1 . 2 -8
      68216247 2420 2210 1 -8 1 1 1 1 2 2 -8
      68218287 2420 2210 -8 -8 2 2 2 2 1 1 -8
      68230527 2444 2222 -8 . . 1 . 2 . 2 -8
      68231223 2444 2222 4 -8 2 2 2 2 2 2 -8
      68238011 2468 2234 1 -8 2 2 1 1 2 2 -8
      68262487 2516 2258 2 -8 2 2 1 1 1 1 -8
      68266567 2516 2258 4 -8 2 2 2 2 2 2 -8
      68278127 2540 2270 4 -8 2 2 2 2 2 2 -8
      68288327 2564 2282 1 -8 1 1 1 1 2 2 -8
      68288331 2564 2282 1 -8 1 1 1 1 1 1 -8
      68291731 2564 2282 4 -8 2 2 2 2 2 2 -8
      68293087 2564 2282 4 -8 1 1 1 1 2 2 -8
      68293091 2564 2282 1 -8 1 1 1 1 1 1 -8
      68293095 2564 2282 1 -8 1 1 1 1 1 1 -8
      68293099 2564 2282 1 -8 2 2 1 1 1 1 -8
      68293168 2108 2054 4 -8 2 2 2 2 1 1 -8
      end
      label values psu psu
      label values strata strata
      label values ce_semp ce_semp
      label def ce_semp -8 "inapplicable", modify
      label def ce_semp 1 "Yes, employed only", modify
      label def ce_semp 2 "Yes, self-employed only", modify
      label def ce_semp 3 "Both employed and self-employed", modify
      label def ce_semp 4 "No", modify
      label values cg_semp cg_semp
      label def cg_semp -8 "inapplicable", modify
      label def cg_semp 2 "Yes, self-employed only", modify
      label def cg_semp 4 "No", modify
      label values cg_parent5plus cg_parent5plus
      label def cg_parent5plus 1 "Yes", modify
      label def cg_parent5plus 2 "No", modify
      label values ce_parent5plus ce_parent5plus
      label def ce_parent5plus 1 "Yes", modify
      label def ce_parent5plus 2 "No", modify
      label values cg_couple cg_couple
      label def cg_couple 1 "Yes", modify
      label def cg_couple 2 "No", modify
      label values ce_couple ce_couple
      label def ce_couple 1 "Yes", modify
      label def ce_couple 2 "No", modify
      label values cg_sex_cv cg_sex_cv
      label def cg_sex_cv 1 "Male", modify
      label def cg_sex_cv 2 "Female", modify
      label values ce_sex_cv ce_sex_cv
      label def ce_sex_cv 1 "Male", modify
      label def ce_sex_cv 2 "Female", modify
      label values i_qfhigh i_qfhigh
      label def i_qfhigh -8 "inapplicable", modify
      label def i_qfhigh 15 "Other school (inc. school leaving exam certificate or matriculation)", modify
      [/CODE]

      Comment


      • #4
        Well, now that I can see your data clearly, I perceive the problem somewhat differently. I am not 100% clear on what you want the result to look like, but see if this is it:

        Code:
        ds ce_* cg_*
        local stublist `r(varlist)'
        local stublist: subinstr local stublist "ce_" "@", all
        local stublist: subinstr local stublist "cg_" "@", all
        local stublist: list uniq stublist
        
        reshape long `stublist', i(pidp) j(_j) string
        This code runs in your example data and produces what might be the kind of result you are looking for. And I ask you to try the code first in your example data to see if that is the case.

        I suspect it will not run correctly in your full data. I say that because your original problem was with an error message mentioning 0_walsh . There is nothing in your example data that would produce a 0_walsh variable. So, this code will probably stumble on the same problem in your real data. If it does, then I suggest that you insert the lines:
        Code:
        foreach s of local stublist {
            local t = strtoname(substr(`"`s'"', 2, .))
            local stublist: subinstr local stublist `"`s'"' `"@`t'"'
        }
        immediately after -local stublist: list uniq stublist-. That should clear up problems of that nature.

        Whether you will falter again on "too many variables" I cannot say, but it would surprise me. When you reshape from wide to long, you reduce the number of variables, and your data set must have an allowable number of variables or you wouldn't have been able to create or use it in the first place. And there is no specific limit on the number of variables -reshape- can handle. Nor do I see how you could, along the way, exceed the number of characters in a command or a macro if you are using the SE or MP flavor. So I'm a little uneasy because I don't understand how that "too many variables" problem arose. But let's see if this works, and if not, we'll try to figure out what to do next.


        Comment


        • #5
          Hello,

          Thank you for your message. I run the command and I didn´t get any error. But I need more thing: In the rown I need to have the information of the variables on "September 2020" and "January 2021" (like a panel data). The variables with information of september 2020 are the ones that start with "ce_" and the variables of january 2021 are the ones that start with "cg_". Thank you help me with this. I send below that example of the data that I obtained aer using your command. Thnak you very much in advance.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input byte(semp parent5plus couple sex_cv i_qfhigh)
           1 2 1 2 -8
           . . . . -8
          -8 . . . -8
          -8 2 1 2 -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           1 . . . -8
           . . . . -8
          -8 . . . -8
          -8 . . . -8
           1 . . . -8
          -8 . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
          -8 1 1 2 -8
           . . . . -8
           2 . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           1 . . . -8
           . . . . -8
          -8 . . . -8
          -8 . . . -8
           1 . . . -8
          -8 . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
          -8 2 2 2 15
           . . . . 15
           1 . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
          -9 . . . 15
           . . . . 15
           1 . . . 15
          -8 . . . 15
           1 . . . 15
          -8 . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
           . . . . 15
          -8 2 2 1 -8
           . . . . -8
           4 . . . -8
          -8 2 2 1 -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
          -9 . . . -8
           . . . . -8
          -8 . . . -8
          -8 . . . -8
          -8 . . . -8
          -8 . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
           3 2 2 2 -8
           . . . . -8
          -8 . . . -8
          -8 2 2 2 -8
           . . . . -8
           . . . . -8
           . . . . -8
           . . . . -8
          end
          label values i_qfhigh i_qfhigh
          label def i_qfhigh -8 "inapplicable", modify
          label def i_qfhigh 15 "Other school (inc. school leaving exam certificate or matriculation)", modify

          Comment


          • #6
            Code:
            gen int date = cond(_j == "ce_", tm(2021m1), tm(2020m9))
            format date %tmMonth_CCYY
            
            drop _j
            should do it.

            Comment


            • #7
              Dear Clyde,

              Thank you for your message. I run your command but there is something wrong: I have a dataset with information for both september 2020 (the variables that start with ce_) and january 2021 (the variables that start with cg_). When I run your command I miss almost all the data from january 2022.
              Can you help me with this? I send below the example of the data that I obtained. Thank you very much in advance.
              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input long(pidp psu) int(strata date) byte(semp parent5plus couple sex_cv i_qfhigh)
                76165  19  6 732  1 2 1 2 -8
                76165  19  6 728  . . . . -8
                76165  19  6 728 -8 . . . -8
                76165  19  6 728 -8 2 1 2 -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  1 . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728 -8 . . . -8
                76165  19  6 728 -8 . . . -8
                76165  19  6 728  1 . . . -8
                76165  19  6 728 -8 . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
                76165  19  6 728  . . . . -8
               280165  67 15 732 -8 1 1 2 -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  2 . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  1 . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728 -8 . . . -8
               280165  67 15 728 -8 . . . -8
               280165  67 15 728  1 . . . -8
               280165  67 15 728 -8 . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               280165  67 15 728  . . . . -8
               469205 106 25 732 -8 2 2 2 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  1 . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728 -9 . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  1 . . . 15
               469205 106 25 728 -8 . . . 15
               469205 106 25 728  1 . . . 15
               469205 106 25 728 -8 . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               469205 106 25 728  . . . . 15
               732365 157 43 732 -8 2 2 1 -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  4 . . . -8
               732365 157 43 728 -8 2 2 1 -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728 -9 . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728 -8 . . . -8
               732365 157 43 728 -8 . . . -8
               732365 157 43 728 -8 . . . -8
               732365 157 43 728 -8 . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
               732365 157 43 728  . . . . -8
              1587125 215 65 732  3 2 2 2 -8
              1587125 215 65 728  . . . . -8
              1587125 215 65 728 -8 . . . -8
              1587125 215 65 728 -8 2 2 2 -8
              1587125 215 65 728  . . . . -8
              1587125 215 65 728  . . . . -8
              1587125 215 65 728  . . . . -8
              1587125 215 65 728  . . . . -8
              end
              format %tmMonth_CCYY date
              label values psu psu
              label values strata strata
              label values i_qfhigh i_qfhigh
              label def i_qfhigh -8 "inapplicable", modify
              label def i_qfhigh 15 "Other school (inc. school leaving exam certificate or matriculation)", modify

              Comment


              • #8
                Sending me the results illustrates what you are seeing, but doesn't help me solve the problem. With the example data you gave in #3, I ran the code from #4 and #6, and it produces correct results. So you need to post new example data that exhibits the problem you are encountering.

                Comment


                • #9
                  Dear Clyde,

                  Below is the dataset before any command. I wrote the command for dataex with only some of the variables because the dataset in very large. I use the command "set maxvar 32767, permanently" to be able to use all the variables in the dataset. The goal is to reshape all the variables that begin with "ce_" and "cg_" and give them the names "September 2020" and "January 2021", respectively. Variable that begin with "i_" must be repeated for each observation. Thank you very much in advance.

                  ​​​​​​
                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input long(pidp psu) int strata long(i_hidp j_hidp k_hidp) byte(cg_semp ce_semp cg_parent5plus ce_parent5plus ce_couple cg_couple ce_sex_cv cg_sex_cv i_qfhigh)
                     76165   19    6 141657616 141460418 141045620 -8  1 2 2 1 1 2 2 -8
                    280165   67   15 754793216 754371618 754113220  . -8 . 1 1 . 2 . -8
                    469205  106   25 415059096 414738818 414412420  . -8 . 2 2 . 2 . 15
                    732365  157   43 619371216 618949618 618698020 -8 -8 2 2 2 2 1 1 -8
                   1587125  215   65 618269616 617895618 617671220 -8  3 2 2 2 2 2 2 -8
                   4849085  560  148 347554816 347255618 346990420 -8  1 2 2 1 1 1 1 -8
                  68002725    1    1  73025216  72739618  72420020 -8  4 2 2 2 2 2 2 -8
                  68008847 2012 2006  68040816  68040818  68027220 -8  1 2 2 2 2 2 2 -8
                  68010887 2012 2006  68054416  68054418  68040820 -8  1 2 2 1 1 2 2 -8
                  68029931 2060 2030  68136016  68129218  68102020 -8  . 1 . . 1 . 1 -8
                  68031967 2060 2030  68142816  68136018  68108820  4  4 2 2 2 2 2 2 -8
                  68035365   11    4  73045616  72760018  72433620 -8  4 2 2 2 2 1 1 -8
                  68035367 2060 2030  68156416  68149618  68115620 -8  1 2 2 1 1 1 1 -8
                  68041487 2084 2042  68176816  68170018  68142820 -8  1 1 1 1 1 2 2 -8
                  68041491 2084 2042  68176816  68170018  68142820 -8  . 1 . . 1 . 1 -8
                  68045567 2084 2042  68210816  68204018  68177500 -8  1 2 2 2 2 2 2 -8
                  68051007 2108 2054  68251616  68244818  68204020 -8  2 2 2 1 1 1 1 -8
                  68051011 2108 2054  68251616  68244818  68204020 -8  1 2 2 1 1 2 2 -8
                  68058487 2108 2054  68272016  68265218  68224420 -8  4 2 2 1 1 1 1 -8
                  68058491 2108 2054  68272016  68265218  68224420 -8  4 2 2 1 1 2 2 -8
                  68060531 2108 2054  68285616  68278818  68238020 -8  1 1 1 1 1 2 2 -8
                  68060533   18    6  73059216  72773618        -9 -8  4 2 2 1 1 2 2 -8
                  68060537   18    6  73059216  72773618        -9 -8  4 2 2 1 1 1 1 -8
                  68061288 2012 2006  68047616  68047618  68034020  . -8 . 2 1 . 2 . -8
                  68063247 2132 2066  68292416  68285618  68244820 -8  1 1 1 1 1 2 2 -8
                  68063927 2132 2066  68299216  68292418  68251620 -8  1 1 1 1 1 2 2 -8
                  68063931 2132 2066  68299216  68292418  68251620 -8 -8 1 1 1 1 1 1 -8
                  68064605   18    6  73066016  72780418  72454020 -8  4 2 2 1 1 1 1 -8
                  68064609   18    6  73066016  72780418  72454020 -8  4 2 2 1 1 2 2 -8
                  68068007 2132 2066  68326416  68312818  68272020 -8  1 2 2 1 1 1 1 -8
                  68068011 2132 2066  68326416  68312818  68272020  .  1 . 2 2 . 2 . -8
                  68068082 2012 2006  68054416  68054418  68040820 -8  2 2 2 1 1 1 1 -8
                  68097245   25    8  73072816  72787218  72460820 -8  4 2 2 2 2 2 2 -8
                  68097927 2180 2090  68421616  68414818  68353620 -8  4 2 2 2 2 2 2 -8
                  68112211 2228 2114  68462416  68448818  68380820  .  1 . 1 1 . 2 . -8
                  68120367 2228 2114  68476016  68462418  68394420 -8  4 2 2 2 2 2 2 -8
                  68120375 2228 2114  68476696  68469218  68401220 -8  1 1 2 2 2 2 2 -8
                  68125127 2252 2126  68516816  68503218  68428420 -8  1 1 1 1 1 2 2 -8
                  68125131 2252 2126  68516816  68503218  68428420 -8  1 2 2 1 1 1 1 -8
                  68125135 2252 2126  68516816  68503218  68428420 -8  1 2 2 2 2 2 2 -8
                  68133285   34   11  73086416  72800818  72474420 -8  4 2 2 2 2 2 2 -8
                  68133289   34   11  73093216  72807618  72481220 -8  4 2 2 1 1 2 2 -8
                  68136009   34   11  73106816  72821218  72494820 -8  1 1 1 2 2 2 2 -8
                  68137365   34   11  73113616  72828018  72501620 -8 -8 2 2 2 2 2 2 -8
                  68138045   34   11  73120416  72834818  72508420 -8  4 2 2 1 1 1 1 -8
                  68138049   34   11  73120416  72834818  72508420 -8  4 2 2 1 1 2 2 -8
                  68138051 2276 2138  68550816  68544018  68469220 -8  4 2 2 1 1 2 2 -8
                  68144847 2276 2138  68584816  68578018  68503220 -8  1 2 2 1 1 1 1 -8
                  68144851 2276 2138  68584816  68578018  68503220 -8  1 1 1 1 1 2 2 -8
                  68148247 2300 2150  68591616  68584818  68510020 -8 -8 2 2 1 1 1 1 -8
                  68148251 2300 2150  68591616  68584818  68510020  .  4 . 2 1 . 2 . -8
                  68150967 2300 2150  68598416  68591618  68516820  .  1 . 2 1 . 1 . -8
                  68150971 2300 2150  68598416  68591618  68516820 -8  1 2 2 1 1 2 2 -8
                  68150975 2300 2150  68598416  68591618  68516820 -8  1 2 2 2 2 1 1 -8
                  68155047 2300 2150  68618816  68605218  68530420  4 -8 1 1 1 1 2 2 -8
                  68155051 2300 2150  68618816  68605218  68530420  4  1 1 1 1 1 1 1 -8
                  68157771 2300 2150  68666416  68632418  68557620  2 -8 2 2 2 2 2 2 -8
                  68159131 2300 2150  68673216  68639218  68564420 -8  1 1 1 1 1 2 2 -8
                  68160485   39   11  73127216  72841618  72515220 -8  1 1 1 2 2 2 2 -8
                  68160489   39   11  73134016  72848418  72522020 -8  1 2 2 2 2 1 1 -8
                  68173407 2348 2174  68707216  68673218  68598420 -8  4 2 2 2 1 2 2 -8
                  68174767 2348 2174  68720816  68686818  68612020  .  4 . 1 1 . 2 . -8
                  68180887 2348 2174  68754816  68714018  68632420 -8  1 1 1 1 1 2 2 -8
                  68180891 2348 2174  68754816  68714018  68632420  .  1 . 1 1 . 1 . -8
                  68184971 2372 2186  68761616  68720818  68639220 -8  . 1 . . 1 . 2 -8
                  68185647 2372 2186  68768416  68727618  68646020 -8  4 2 2 2 2 2 2 -8
                  68187687 2372 2186  68775216  68734418  68659620 -8  4 1 1 1 1 1 1 -8
                  68187691 2372 2186  68775216  68734418  68659620 -8  4 1 1 1 1 2 2 -8
                  68191771 2372 2186  68809216  68775218        -9 -8  1 1 1 2 2 2 2 -8
                  68193127 2372 2186  68816016  68782018  68707220 -8  4 1 1 2 2 2 2 -8
                  68195167 2372 2186  68822816  68795618  68720820 -8  4 2 2 1 1 1 1 -8
                  68195171 2372 2186  68822816  68795618  68720820 -8  4 2 2 1 1 2 2 -8
                  68195851 2372 2186  68829616  68802418  68727620 -8  1 2 2 1 1 2 2 -8
                  68197211 2396 2198  68836416  68809218  68734420  .  1 . 2 1 . 2 . -8
                  68197887 2396 2198  68843216  68816018  68741220 -8  1 1 1 2 2 2 2 -8
                  68197899 2396 2198  68843216  68817378  68754820 -8 -8 2 1 2 2 2 2 -8
                  68197903 2396 2198  68843216  68816018  68741220 -8 -8 2 2 2 2 1 1 -8
                  68199247 2396 2198  76506816  76153218  75595620 -8  1 2 2 1 1 1 1 -8
                  68207407 2396 2198  68863616  68836418  68775220 -8  4 2 2 1 1 2 2 -8
                  68207411 2396 2198  68863616  68836418  68775220 -8  4 2 2 1 1 1 1 -8
                  68211487 2420 2210  68877216  68850018  68788820 -8  4 2 2 2 2 1 1 -8
                  68214207 2420 2210  68890816  68863618  68802420 -8  1 2 2 2 2 1 1 -8
                  68214887 2420 2210  68897616  68870418  68809220  . -8 . 1 1 . 1 . -8
                  68214891 2420 2210  68897616  68870418  68809220  .  1 . 1 1 . 2 . -8
                  68216247 2420 2210  68904416  68877218  68816020 -8  1 1 1 1 1 2 2 -8
                  68218287 2420 2210  68911216  68884018  68822820 -8 -8 2 2 2 2 1 1 -8
                  68230527 2444 2222  68979216  68965618  68897620  . -8 . 1 2 . 2 . -8
                  68231223 2444 2222  68986016  68972418  68904420 -8  4 2 2 2 2 2 2 -8
                  68238011 2468 2234  69006416  68986018  68911220 -8  1 2 2 1 1 2 2 -8
                  68262487 2516 2258  69081216  69047218  68965620 -8  2 2 2 1 1 1 1 -8
                  68266567 2516 2258  69101616  69067618  68986020 -8  4 2 2 2 2 2 2 -8
                  68278127 2540 2270  69142416  69108418  69020020 -8  4 2 2 2 2 2 2 -8
                  68288327 2564 2282  69162816  69128818  69033620 -8  1 1 1 1 1 2 2 -8
                  68288331 2564 2282  69162816  69128818  69033620 -8  1 1 1 1 1 1 1 -8
                  68291731 2564 2282  69169616  69135618  69040420 -8  4 2 2 2 2 2 2 -8
                  68293087 2564 2282  69176416  69142418  69047220 -8  4 1 1 1 1 2 2 -8
                  68293091 2564 2282  69176416  69142418  69047220 -8  1 1 1 1 1 1 1 -8
                  68293095 2564 2282  69176416  69143098  69054020 -8  1 1 1 1 1 1 1 -8
                  68293099 2564 2282  69176416  69142418  69047220 -8  1 2 2 1 1 1 1 -8
                  68293168 2108 2054  68278816  68272018        -9 -8  4 2 2 2 2 1 1 -8
                  end
                  label values psu psu
                  label values strata strata
                  label values cg_semp cg_semp
                  label def cg_semp -8 "inapplicable", modify
                  label def cg_semp 2 "Yes, self-employed only", modify
                  label def cg_semp 4 "No", modify
                  label values ce_semp ce_semp
                  label def ce_semp -8 "inapplicable", modify
                  label def ce_semp 1 "Yes, employed only", modify
                  label def ce_semp 2 "Yes, self-employed only", modify
                  label def ce_semp 3 "Both employed and self-employed", modify
                  label def ce_semp 4 "No", modify
                  label values cg_parent5plus cg_parent5plus
                  label def cg_parent5plus 1 "Yes", modify
                  label def cg_parent5plus 2 "No", modify
                  label values ce_parent5plus ce_parent5plus
                  label def ce_parent5plus 1 "Yes", modify
                  label def ce_parent5plus 2 "No", modify
                  label values ce_couple ce_couple
                  label def ce_couple 1 "Yes", modify
                  label def ce_couple 2 "No", modify
                  label values cg_couple cg_couple
                  label def cg_couple 1 "Yes", modify
                  label def cg_couple 2 "No", modify
                  label values ce_sex_cv ce_sex_cv
                  label def ce_sex_cv 1 "Male", modify
                  label def ce_sex_cv 2 "Female", modify
                  label values cg_sex_cv cg_sex_cv
                  label def cg_sex_cv 1 "Male", modify
                  label def cg_sex_cv 2 "Female", modify
                  label values i_qfhigh i_qfhigh
                  label def i_qfhigh -8 "inapplicable", modify
                  label def i_qfhigh 15 "Other school (inc. school leaving exam certificate or matriculation)", modify

                  The commands than I runned were:

                  ds ce_* cg_*
                  local stublist `r(varlist)'
                  local stublist: subinstr local stublist "ce_" "@", all
                  local stublist: subinstr local stublist "cg_" "@", all
                  local stublist: list uniq stublist

                  reshape long `stublist', i(pidp) j(_j) string

                  foreach s of local stublist {
                  local t = strtoname(substr(`"`s'"', 2, .))
                  local stublist: subinstr local stublist `"`s'"' `"@`t'"'
                  }
                  drop if psu==.

                  gen int date = cond(_j == "ce_", tm(2021m1), tm(2020m9))
                  format date %tmMonth_CCYY

                  drop _j


                  After running this command almost all the observations from january 2021 disappear.


                  Last edited by Ana Vasconcelos; 26 Mar 2022, 15:29.

                  Comment


                  • #10
                    The new example data does not demonstrate the problem you raise in #7. As you can see, with this example data, the code runs without error messages, and, in the end, you have equal numbers of observations with the two dates:

                    Code:
                    . * Example generated by -dataex-. For more info, type help dataex
                    . clear
                    
                    . input long(pidp psu) int strata long(i_hidp j_hidp k_hidp) byte(cg_semp ce_semp cg_parent5plus ce_parent5plus ce_couple cg_couple ce_sex
                    > _cv cg_sex_cv i_qfhigh)
                    
                                 pidp           psu    strata        i_hidp        j_hidp        k_hidp   cg_semp   ce_semp  cg_par~s  ce_par~s  ce_cou~e  cg_
                    > cou~e  ce_sex~v  cg_sex~v  i_qfhigh
                      1.    76165   19    6 141657616 141460418 141045620 -8  1 2 2 1 1 2 2 -8
                      2.   280165   67   15 754793216 754371618 754113220  . -8 . 1 1 . 2 . -8
                      3.   469205  106   25 415059096 414738818 414412420  . -8 . 2 2 . 2 . 15
                      4.   732365  157   43 619371216 618949618 618698020 -8 -8 2 2 2 2 1 1 -8
                      5.  1587125  215   65 618269616 617895618 617671220 -8  3 2 2 2 2 2 2 -8
                      6.  4849085  560  148 347554816 347255618 346990420 -8  1 2 2 1 1 1 1 -8
                      7. 68002725    1    1  73025216  72739618  72420020 -8  4 2 2 2 2 2 2 -8
                      8. 68008847 2012 2006  68040816  68040818  68027220 -8  1 2 2 2 2 2 2 -8
                      9. 68010887 2012 2006  68054416  68054418  68040820 -8  1 2 2 1 1 2 2 -8
                     10. 68029931 2060 2030  68136016  68129218  68102020 -8  . 1 . . 1 . 1 -8
                     11. 68031967 2060 2030  68142816  68136018  68108820  4  4 2 2 2 2 2 2 -8
                     12. 68035365   11    4  73045616  72760018  72433620 -8  4 2 2 2 2 1 1 -8
                     13. 68035367 2060 2030  68156416  68149618  68115620 -8  1 2 2 1 1 1 1 -8
                     14. 68041487 2084 2042  68176816  68170018  68142820 -8  1 1 1 1 1 2 2 -8
                     15. 68041491 2084 2042  68176816  68170018  68142820 -8  . 1 . . 1 . 1 -8
                     16. 68045567 2084 2042  68210816  68204018  68177500 -8  1 2 2 2 2 2 2 -8
                     17. 68051007 2108 2054  68251616  68244818  68204020 -8  2 2 2 1 1 1 1 -8
                     18. 68051011 2108 2054  68251616  68244818  68204020 -8  1 2 2 1 1 2 2 -8
                     19. 68058487 2108 2054  68272016  68265218  68224420 -8  4 2 2 1 1 1 1 -8
                     20. 68058491 2108 2054  68272016  68265218  68224420 -8  4 2 2 1 1 2 2 -8
                     21. 68060531 2108 2054  68285616  68278818  68238020 -8  1 1 1 1 1 2 2 -8
                     22. 68060533   18    6  73059216  72773618        -9 -8  4 2 2 1 1 2 2 -8
                     23. 68060537   18    6  73059216  72773618        -9 -8  4 2 2 1 1 1 1 -8
                     24. 68061288 2012 2006  68047616  68047618  68034020  . -8 . 2 1 . 2 . -8
                     25. 68063247 2132 2066  68292416  68285618  68244820 -8  1 1 1 1 1 2 2 -8
                     26. 68063927 2132 2066  68299216  68292418  68251620 -8  1 1 1 1 1 2 2 -8
                     27. 68063931 2132 2066  68299216  68292418  68251620 -8 -8 1 1 1 1 1 1 -8
                     28. 68064605   18    6  73066016  72780418  72454020 -8  4 2 2 1 1 1 1 -8
                     29. 68064609   18    6  73066016  72780418  72454020 -8  4 2 2 1 1 2 2 -8
                     30. 68068007 2132 2066  68326416  68312818  68272020 -8  1 2 2 1 1 1 1 -8
                     31. 68068011 2132 2066  68326416  68312818  68272020  .  1 . 2 2 . 2 . -8
                     32. 68068082 2012 2006  68054416  68054418  68040820 -8  2 2 2 1 1 1 1 -8
                     33. 68097245   25    8  73072816  72787218  72460820 -8  4 2 2 2 2 2 2 -8
                     34. 68097927 2180 2090  68421616  68414818  68353620 -8  4 2 2 2 2 2 2 -8
                     35. 68112211 2228 2114  68462416  68448818  68380820  .  1 . 1 1 . 2 . -8
                     36. 68120367 2228 2114  68476016  68462418  68394420 -8  4 2 2 2 2 2 2 -8
                     37. 68120375 2228 2114  68476696  68469218  68401220 -8  1 1 2 2 2 2 2 -8
                     38. 68125127 2252 2126  68516816  68503218  68428420 -8  1 1 1 1 1 2 2 -8
                     39. 68125131 2252 2126  68516816  68503218  68428420 -8  1 2 2 1 1 1 1 -8
                     40. 68125135 2252 2126  68516816  68503218  68428420 -8  1 2 2 2 2 2 2 -8
                     41. 68133285   34   11  73086416  72800818  72474420 -8  4 2 2 2 2 2 2 -8
                     42. 68133289   34   11  73093216  72807618  72481220 -8  4 2 2 1 1 2 2 -8
                     43. 68136009   34   11  73106816  72821218  72494820 -8  1 1 1 2 2 2 2 -8
                     44. 68137365   34   11  73113616  72828018  72501620 -8 -8 2 2 2 2 2 2 -8
                     45. 68138045   34   11  73120416  72834818  72508420 -8  4 2 2 1 1 1 1 -8
                     46. 68138049   34   11  73120416  72834818  72508420 -8  4 2 2 1 1 2 2 -8
                     47. 68138051 2276 2138  68550816  68544018  68469220 -8  4 2 2 1 1 2 2 -8
                     48. 68144847 2276 2138  68584816  68578018  68503220 -8  1 2 2 1 1 1 1 -8
                     49. 68144851 2276 2138  68584816  68578018  68503220 -8  1 1 1 1 1 2 2 -8
                     50. 68148247 2300 2150  68591616  68584818  68510020 -8 -8 2 2 1 1 1 1 -8
                     51. 68148251 2300 2150  68591616  68584818  68510020  .  4 . 2 1 . 2 . -8
                     52. 68150967 2300 2150  68598416  68591618  68516820  .  1 . 2 1 . 1 . -8
                     53. 68150971 2300 2150  68598416  68591618  68516820 -8  1 2 2 1 1 2 2 -8
                     54. 68150975 2300 2150  68598416  68591618  68516820 -8  1 2 2 2 2 1 1 -8
                     55. 68155047 2300 2150  68618816  68605218  68530420  4 -8 1 1 1 1 2 2 -8
                     56. 68155051 2300 2150  68618816  68605218  68530420  4  1 1 1 1 1 1 1 -8
                     57. 68157771 2300 2150  68666416  68632418  68557620  2 -8 2 2 2 2 2 2 -8
                     58. 68159131 2300 2150  68673216  68639218  68564420 -8  1 1 1 1 1 2 2 -8
                     59. 68160485   39   11  73127216  72841618  72515220 -8  1 1 1 2 2 2 2 -8
                     60. 68160489   39   11  73134016  72848418  72522020 -8  1 2 2 2 2 1 1 -8
                     61. 68173407 2348 2174  68707216  68673218  68598420 -8  4 2 2 2 1 2 2 -8
                     62. 68174767 2348 2174  68720816  68686818  68612020  .  4 . 1 1 . 2 . -8
                     63. 68180887 2348 2174  68754816  68714018  68632420 -8  1 1 1 1 1 2 2 -8
                     64. 68180891 2348 2174  68754816  68714018  68632420  .  1 . 1 1 . 1 . -8
                     65. 68184971 2372 2186  68761616  68720818  68639220 -8  . 1 . . 1 . 2 -8
                     66. 68185647 2372 2186  68768416  68727618  68646020 -8  4 2 2 2 2 2 2 -8
                     67. 68187687 2372 2186  68775216  68734418  68659620 -8  4 1 1 1 1 1 1 -8
                     68. 68187691 2372 2186  68775216  68734418  68659620 -8  4 1 1 1 1 2 2 -8
                     69. 68191771 2372 2186  68809216  68775218        -9 -8  1 1 1 2 2 2 2 -8
                     70. 68193127 2372 2186  68816016  68782018  68707220 -8  4 1 1 2 2 2 2 -8
                     71. 68195167 2372 2186  68822816  68795618  68720820 -8  4 2 2 1 1 1 1 -8
                     72. 68195171 2372 2186  68822816  68795618  68720820 -8  4 2 2 1 1 2 2 -8
                     73. 68195851 2372 2186  68829616  68802418  68727620 -8  1 2 2 1 1 2 2 -8
                     74. 68197211 2396 2198  68836416  68809218  68734420  .  1 . 2 1 . 2 . -8
                     75. 68197887 2396 2198  68843216  68816018  68741220 -8  1 1 1 2 2 2 2 -8
                     76. 68197899 2396 2198  68843216  68817378  68754820 -8 -8 2 1 2 2 2 2 -8
                     77. 68197903 2396 2198  68843216  68816018  68741220 -8 -8 2 2 2 2 1 1 -8
                     78. 68199247 2396 2198  76506816  76153218  75595620 -8  1 2 2 1 1 1 1 -8
                     79. 68207407 2396 2198  68863616  68836418  68775220 -8  4 2 2 1 1 2 2 -8
                     80. 68207411 2396 2198  68863616  68836418  68775220 -8  4 2 2 1 1 1 1 -8
                     81. 68211487 2420 2210  68877216  68850018  68788820 -8  4 2 2 2 2 1 1 -8
                     82. 68214207 2420 2210  68890816  68863618  68802420 -8  1 2 2 2 2 1 1 -8
                     83. 68214887 2420 2210  68897616  68870418  68809220  . -8 . 1 1 . 1 . -8
                     84. 68214891 2420 2210  68897616  68870418  68809220  .  1 . 1 1 . 2 . -8
                     85. 68216247 2420 2210  68904416  68877218  68816020 -8  1 1 1 1 1 2 2 -8
                     86. 68218287 2420 2210  68911216  68884018  68822820 -8 -8 2 2 2 2 1 1 -8
                     87. 68230527 2444 2222  68979216  68965618  68897620  . -8 . 1 2 . 2 . -8
                     88. 68231223 2444 2222  68986016  68972418  68904420 -8  4 2 2 2 2 2 2 -8
                     89. 68238011 2468 2234  69006416  68986018  68911220 -8  1 2 2 1 1 2 2 -8
                     90. 68262487 2516 2258  69081216  69047218  68965620 -8  2 2 2 1 1 1 1 -8
                     91. 68266567 2516 2258  69101616  69067618  68986020 -8  4 2 2 2 2 2 2 -8
                     92. 68278127 2540 2270  69142416  69108418  69020020 -8  4 2 2 2 2 2 2 -8
                     93. 68288327 2564 2282  69162816  69128818  69033620 -8  1 1 1 1 1 2 2 -8
                     94. 68288331 2564 2282  69162816  69128818  69033620 -8  1 1 1 1 1 1 1 -8
                     95. 68291731 2564 2282  69169616  69135618  69040420 -8  4 2 2 2 2 2 2 -8
                     96. 68293087 2564 2282  69176416  69142418  69047220 -8  4 1 1 1 1 2 2 -8
                     97. 68293091 2564 2282  69176416  69142418  69047220 -8  1 1 1 1 1 1 1 -8
                     98. 68293095 2564 2282  69176416  69143098  69054020 -8  1 1 1 1 1 1 1 -8
                     99. 68293099 2564 2282  69176416  69142418  69047220 -8  1 2 2 1 1 1 1 -8
                    100. 68293168 2108 2054  68278816  68272018        -9 -8  4 2 2 2 2 1 1 -8
                    101. end
                    
                    . label values psu psu
                    
                    . label values strata strata
                    
                    . label values cg_semp cg_semp
                    
                    . label def cg_semp -8 "inapplicable", modify
                    
                    . label def cg_semp 2 "Yes, self-employed only", modify
                    
                    . label def cg_semp 4 "No", modify
                    
                    . label values ce_semp ce_semp
                    
                    . label def ce_semp -8 "inapplicable", modify
                    
                    . label def ce_semp 1 "Yes, employed only", modify
                    
                    . label def ce_semp 2 "Yes, self-employed only", modify
                    
                    . label def ce_semp 3 "Both employed and self-employed", modify
                    
                    . label def ce_semp 4 "No", modify
                    
                    . label values cg_parent5plus cg_parent5plus
                    
                    . label def cg_parent5plus 1 "Yes", modify
                    
                    . label def cg_parent5plus 2 "No", modify
                    
                    . label values ce_parent5plus ce_parent5plus
                    
                    . label def ce_parent5plus 1 "Yes", modify
                    
                    . label def ce_parent5plus 2 "No", modify
                    
                    . label values ce_couple ce_couple
                    
                    . label def ce_couple 1 "Yes", modify
                    
                    . label def ce_couple 2 "No", modify
                    
                    . label values cg_couple cg_couple
                    
                    . label def cg_couple 1 "Yes", modify
                    
                    . label def cg_couple 2 "No", modify
                    
                    . label values ce_sex_cv ce_sex_cv
                    
                    . label def ce_sex_cv 1 "Male", modify
                    
                    . label def ce_sex_cv 2 "Female", modify
                    
                    . label values cg_sex_cv cg_sex_cv
                    
                    . label def cg_sex_cv 1 "Male", modify
                    
                    . label def cg_sex_cv 2 "Female", modify
                    
                    . label values i_qfhigh i_qfhigh
                    
                    . label def i_qfhigh -8 "inapplicable", modify
                    
                    . label def i_qfhigh 15 "Other school (inc. school leaving exam certificate or matriculation)", modify
                    
                    .
                    . ds ce_* cg_*
                    ce_semp       ce_parent5~s  ce_couple     ce_sex_cv     cg_semp       cg_parent5~s  cg_couple     cg_sex_cv
                    
                    . local stublist `r(varlist)'
                    
                    . local stublist: subinstr local stublist "ce_" "@", all
                    
                    . local stublist: subinstr local stublist "cg_" "@", all
                    
                    . local stublist: list uniq stublist
                    
                    .
                    . foreach s of local stublist {
                      2.     local t = strtoname(substr(`"`s'"', 2, .))
                      3.     local stublist: subinstr local stublist `"`s'"' `"@`t'"'
                      4. }
                    
                    .
                    . display `"`stublist'"'
                    @semp @parent5plus @couple @sex_cv
                    
                    .
                    . reshape long `stublist', i(pidp) j(_j) string
                    (j = ce_ cg_)
                    
                    Data                               Wide   ->   Long
                    -----------------------------------------------------------------------------
                    Number of observations              100   ->   200         
                    Number of variables                  15   ->   12          
                    j variable (2 values)                     ->   _j
                    xij variables:
                                            ce_semp cg_semp   ->   semp
                              ce_parent5plus cg_parent5plus   ->   parent5plus
                                        ce_couple cg_couple   ->   couple
                                        ce_sex_cv cg_sex_cv   ->   sex_cv
                    -----------------------------------------------------------------------------
                    
                    .
                    . gen int date = cond(_j == "ce_", tm(2021m1), tm(2020m9))
                    
                    . format date %tmMonth_CCYY
                    
                    .
                    . drop _j
                    
                    .
                    .
                    end of do-file
                    
                    . tab date
                    
                           date |      Freq.     Percent        Cum.
                    ------------+-----------------------------------
                         2020m9 |        100       50.00       50.00
                         2021m1 |        100       50.00      100.00
                    ------------+-----------------------------------
                          Total |        200      100.00
                    Without being able to reproduce your problem, I can't help you.

                    Comment


                    • #11
                      Dear Clyde,

                      I remove the variables that I didn´t need, I run the commands again and it worked.
                      Thank you very much for all your help.

                      Comment


                      • #12
                        Dear Clyde,

                        I just have one more doubt. Is it the data reshaped so that the variables with "ce" are the ones related to "September 2020" and the variables that start with "cg" the ones related to " January 2021"?
                        Thank you in advance.

                        Comment


                        • #13
                          No, it's the other way around: ce <==> "January 2021" and cg <==> "September 2020". Did I get it backwards? If so, my apologies. But it's very easy to fix:

                          Code:
                          Change
                          gen int date = cond(_j == "ce_", tm(2021m1), tm(2020m9))
                          
                          to
                          gen int date = cond(_j == "cg_", tm(2021m1), tm(2020m9))

                          Comment


                          • #14
                            Dear Clyde,

                            Thank you very much. I’m going to change this last command.

                            Comment


                            • #15
                              Dear Clyde,

                              When I use the commands above to reshape the data I miss all the information of the labels of the variables. It only appear the numbers without any other identification (label). Is it possible to keep the label information and reshape the data at the same time? Thank you very mcuh on advance.

                              Comment

                              Working...
                              X