Announcement

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

  • looping n+1

    Hello I am trying to loop across multiple variables to replace missing variables at earlier time points in a data set with the value of the variable at the last time point in a long file.

    The code I have tried is below:

    foreach var of varlist ra_inquire - ra_gpdoc___2) {
    replace `var'(n+1) if missing(`var') & record_id == record_id
    }

    The error message: factor-variable and time-series operators not allowed

    Thank you for helping me solve the looping code in this case.....


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(ra_inquire ra_gpdose ra_fxstat ra_gpdoseq___1 ra_gpdoseq___2 ra_gpdoseq___3 ra_gpdoseq___4 ra_gpdoseq___5 ra_gpdoseq___6 ra_gpdoseq___7 ra_gpdoseq___8 ra_gpdoseqoth ra_gpdoc___1 ra_gpdoc___2)
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 1 0 1 0 1 0 1 0 . 1 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 1 0 0 0 0 1 0 0 . 0 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 1 1 1 1 1 1 1 0 . 1 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 1 0 0 1 0 1 1 0 . 1 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 1 0 1 1 1 0 1 0 . 1 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 1 0 0 1 1 1 1 0 . 1 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 0 1 0 1 1 0 0 0 . 1 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 0 0 0 0 1 0 1 0 . 0 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 0 0 0 0 0 1 1 0 . 0 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 0 1 0 0 1 1 0 0 0 . 1 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 1 1 1 1 1 1 1 0 . 1 1
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 1 0 1 1 1 1 1 0 . 1 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    1 1 1 1 0 1 1 1 1 0 0 . 1 0
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    . . . . . . . . . . . . . .
    end
    label values ra_inquire ra_inquire_
    label def ra_inquire_ 1 "Yes", modify
    label values ra_gpdose ra_gpdose_
    label def ra_gpdose_ 1 "Yes", modify
    label values ra_fxstat ra_fxstat_
    label def ra_fxstat_ 0 "No", modify
    label def ra_fxstat_ 1 "Yes", modify
    label values ra_gpdoseq___1 ra_gpdoseq___1_
    label def ra_gpdoseq___1_ 0 "Unchecked", modify
    label def ra_gpdoseq___1_ 1 "Checked", modify
    label values ra_gpdoseq___2 ra_gpdoseq___2_
    label def ra_gpdoseq___2_ 0 "Unchecked", modify
    label def ra_gpdoseq___2_ 1 "Checked", modify
    label values ra_gpdoseq___3 ra_gpdoseq___3_
    label def ra_gpdoseq___3_ 0 "Unchecked", modify
    label def ra_gpdoseq___3_ 1 "Checked", modify
    label values ra_gpdoseq___4 ra_gpdoseq___4_
    label def ra_gpdoseq___4_ 0 "Unchecked", modify
    label def ra_gpdoseq___4_ 1 "Checked", modify
    label values ra_gpdoseq___5 ra_gpdoseq___5_
    label def ra_gpdoseq___5_ 0 "Unchecked", modify
    label def ra_gpdoseq___5_ 1 "Checked", modify
    label values ra_gpdoseq___6 ra_gpdoseq___6_
    label def ra_gpdoseq___6_ 0 "Unchecked", modify
    label def ra_gpdoseq___6_ 1 "Checked", modify
    label values ra_gpdoseq___7 ra_gpdoseq___7_
    label def ra_gpdoseq___7_ 0 "Unchecked", modify
    label def ra_gpdoseq___7_ 1 "Checked", modify
    label values ra_gpdoseq___8 ra_gpdoseq___8_
    label def ra_gpdoseq___8_ 0 "Unchecked", modify
    label values ra_gpdoc___1 ra_gpdoc___1_
    label def ra_gpdoc___1_ 0 "Unchecked", modify
    label def ra_gpdoc___1_ 1 "Checked", modify
    label values ra_gpdoc___2 ra_gpdoc___2_
    label def ra_gpdoc___2_ 0 "Unchecked", modify
    label def ra_gpdoc___2_ 1 "Checked", modify

  • #2
    there are a lot of typos and you did not include the record_id variable in your data example; here I have fixed some, at least:
    Code:
    . foreach var of varlist ra_inquire - ra_gpdoc___2 {
      2. replace `var'=`var'[_n+1] if missing(`var') & record_id == record_id[_n+1]
      3. }
    but of course I can't get it to run without the id variable

    you might prefer to download and use -carryforward- a user-written command; use -search- to find and install
    Last edited by Rich Goldstein; 07 Sep 2023, 14:17.

    Comment


    • #3
      Rich has correctly captured the intent of your original code, but I still don't think #2 is quite right, depending on what you're looking for exactly. This will only fill in missing values if they are not missing in the next row. So for example, the second row in your data will be filled in with data from the third row, but the first row will still be missing.

      If you're not happy with -carryforward- you will need to provide example data that includes the record_id.

      Comment


      • #4
        I am really sorry for the typos and not including the subject or time variable. Here is the new data. Daniel, you are correct. I want the 1st and 2nd rows both filled in from the 3rd.....

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte record_id float time_rc byte(ra_inquire ra_gpdose ra_fxstat ra_gpdoseq___1 ra_gpdoseq___2 ra_gpdoseq___3 ra_gpdoseq___4 ra_gpdoseq___5 ra_gpdoseq___6 ra_gpdoseq___7 ra_gpdoseq___8 ra_gpdoseqoth ra_gpdoc___1 ra_gpdoc___2)
         1 0 . . . . . . . . . . . . . .
         1 1 . . . . . . . . . . . . . .
         1 2 1 1 1 1 0 1 0 1 0 1 0 . 1 0
         2 0 . . . . . . . . . . . . . .
         2 1 . . . . . . . . . . . . . .
         2 2 1 1 1 1 0 0 0 0 1 0 0 . 0 0
         3 0 . . . . . . . . . . . . . .
         3 1 . . . . . . . . . . . . . .
         3 2 1 1 1 1 1 1 1 1 1 1 0 . 1 0
         4 0 . . . . . . . . . . . . . .
         4 1 . . . . . . . . . . . . . .
         4 2 1 1 1 1 0 0 1 0 1 1 0 . 1 0
         5 0 . . . . . . . . . . . . . .
         5 1 . . . . . . . . . . . . . .
         5 2 1 1 1 1 0 1 1 1 0 1 0 . 1 0
         6 0 . . . . . . . . . . . . . .
         6 1 . . . . . . . . . . . . . .
         6 2 1 1 1 1 0 0 1 1 1 1 0 . 1 0
         7 0 . . . . . . . . . . . . . .
         7 1 . . . . . . . . . . . . . .
         7 2 1 1 1 0 1 0 1 1 0 0 0 . 1 0
         8 0 . . . . . . . . . . . . . .
         8 1 . . . . . . . . . . . . . .
         8 2 1 1 1 0 0 0 0 1 0 1 0 . 0 0
         9 0 . . . . . . . . . . . . . .
         9 1 . . . . . . . . . . . . . .
         9 2 1 1 1 0 0 0 0 0 1 1 0 . 0 0
        10 0 . . . . . . . . . . . . . .
        10 1 . . . . . . . . . . . . . .
        10 2 1 1 0 1 0 0 1 1 0 0 0 . 1 0
        11 0 . . . . . . . . . . . . . .
        11 1 . . . . . . . . . . . . . .
        11 2 1 1 1 1 1 1 1 1 1 1 0 . 1 1
        12 0 . . . . . . . . . . . . . .
        12 1 . . . . . . . . . . . . . .
        12 2 1 1 1 1 0 1 1 1 1 1 0 . 1 0
        13 0 . . . . . . . . . . . . . .
        13 1 . . . . . . . . . . . . . .
        13 2 1 1 1 1 0 1 1 1 1 0 0 . 1 0
        14 0 . . . . . . . . . . . . . .
        15 0 . . . . . . . . . . . . . .
        16 0 . . . . . . . . . . . . . .
        17 0 . . . . . . . . . . . . . .
        18 0 . . . . . . . . . . . . . .
        end
        label values ra_inquire ra_inquire_
        label def ra_inquire_ 1 "Yes", modify
        label values ra_gpdose ra_gpdose_
        label def ra_gpdose_ 1 "Yes", modify
        label values ra_fxstat ra_fxstat_
        label def ra_fxstat_ 0 "No", modify
        label def ra_fxstat_ 1 "Yes", modify
        label values ra_gpdoseq___1 ra_gpdoseq___1_
        label def ra_gpdoseq___1_ 0 "Unchecked", modify
        label def ra_gpdoseq___1_ 1 "Checked", modify
        label values ra_gpdoseq___2 ra_gpdoseq___2_
        label def ra_gpdoseq___2_ 0 "Unchecked", modify
        label def ra_gpdoseq___2_ 1 "Checked", modify
        label values ra_gpdoseq___3 ra_gpdoseq___3_
        label def ra_gpdoseq___3_ 0 "Unchecked", modify
        label def ra_gpdoseq___3_ 1 "Checked", modify
        label values ra_gpdoseq___4 ra_gpdoseq___4_
        label def ra_gpdoseq___4_ 0 "Unchecked", modify
        label def ra_gpdoseq___4_ 1 "Checked", modify
        label values ra_gpdoseq___5 ra_gpdoseq___5_
        label def ra_gpdoseq___5_ 0 "Unchecked", modify
        label def ra_gpdoseq___5_ 1 "Checked", modify
        label values ra_gpdoseq___6 ra_gpdoseq___6_
        label def ra_gpdoseq___6_ 0 "Unchecked", modify
        label def ra_gpdoseq___6_ 1 "Checked", modify
        label values ra_gpdoseq___7 ra_gpdoseq___7_
        label def ra_gpdoseq___7_ 0 "Unchecked", modify
        label def ra_gpdoseq___7_ 1 "Checked", modify
        label values ra_gpdoseq___8 ra_gpdoseq___8_
        label def ra_gpdoseq___8_ 0 "Unchecked", modify
        label values ra_gpdoc___1 ra_gpdoc___1_
        label def ra_gpdoc___1_ 0 "Unchecked", modify
        label def ra_gpdoc___1_ 1 "Checked", modify
        label values ra_gpdoc___2 ra_gpdoc___2_
        label def ra_gpdoc___2_ 0 "Unchecked", modify
        label def ra_gpdoc___2_ 1 "Checked", modify
        ------------------ copy up to and including the previous line ------------------

        Listed 44 out of 44 observations

        .

        Comment


        • #5
          Thanks for the updated example. Are the non-missing records always in the last record for a given record_id? If so, you just need:


          Code:
          sort record_id time_rc // looks like your data is already sorted like this, but just in case.
          foreach var of varlist ra_inquire - ra_gpdoc___2 {
              bysort record_id: replace `var'=`var'[_N]
          }

          Comment

          Working...
          X