Announcement

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

  • Nested Loop Containing Count of Different Number of Variables Within each wave

    Hi STATA-Peeps,

    I've got 10 waves of data from a panel. Each contains a series of health condition variables [hcondn1....hcondn5...], but they all contain a different number of variables in this series (ranging from hcondn5 to hcondn13).

    I can nest loops within loops, but can someone please help me with the code for only running my foreach loop the right amount of times depending on the wave number please?

    Here is my foreach loop code so far:


    foreach w in b c d e f g h i j k {

    // find the wave number using "String Position" count (strpos)

    local wave_nr=strpos(" bcdefghijk","`w'") // Counts to string posn _234567891011

    local varcount = 0
    foreach hc of varlist `w'_hcondn*{
    varcount =+1
    }

    /* The line below stops the code at the first wave that doesn't have 8 "hcondn" variables in it */

    use id `w'_hcondno1 `w'_hcondno2 `w'_hcondno3 `w'_hcondno4 `w'_hcondno5 `w'_hcondno6 ///
    `w'_hcondno7 `w'_hcondno8 using "$Originals/`w'_indresp", clear

    rename `w'_* *

    gen wave=`wave_nr'

    save temp`w', replace


    Thanks for your help. I have tried a bit, but not quite integrated the two loops quite right.

    Cheers!

    SCE
    Last edited by Sue Sheffield; 04 Feb 2022, 08:13.

  • #2
    You can use wildcards in the varlist on the use command. Try
    Code:
    use id `w'_hcondno* using "$Originals/`w'_indresp", clear
    I am unsure of what you are trying to do with your varcount loop. I don't think it is needed, or else it should appear after the dataset has been read in.
    Last edited by William Lisowski; 04 Feb 2022, 11:39.

    Comment


    • #3
      I agree with William Lisowski.

      The varcount code won't work any way, unless you move some of the code to Mata.

      Code:
      local ++varcount
      would work but you don't need a loop.

      This appears to be the start of what you want, but sorry, I can't follow the rest of it.

      Code:
      forval wave_nr = 2/11 { 
      
          local w = word("`c(alpha)'", `wave_nr') 
      
          * must read n the data before this 
          unab varlist : `w'_hcondn* 
          local varcount :  word count `varlist'

      Comment


      • #4
        Originally posted by William Lisowski View Post
        You can use wildcards in the varlist on the use command. Try
        Code:
        use id `w'_hcondno* using "$Originals/`w'_indresp", clear
        I am unsure of what you are trying to do with your varcount loop. I don't think it is needed, or else it should appear after the dataset has been read in.
        Thanks. I discovered there were also *hcond variables in there but only ion one wave, so had to add those in there as well. But otherwise just simply using `w'_hcond* worked for all the others with different endings.

        Comment

        Working...
        X