Announcement

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

  • How to duplicate values to avoid missing values

    Dear Statalist,

    I've read quite a few posts on my topic https://www.statalist.org/forums/for...-the-dupliates and https://www.statalist.org/forums/for...uence-in-stata but have not found a solution yet.

    I have a randomized order of four treatments with twelve missing values after every 4 treatments (due to some reshaping) - in total 16 lines per individual. I am now looking to duplicate the first number of my treatment 4 times vertically, followed by the second number (also 4 times vertically), followed by the third number (again 4 times vertically), followed by the fourth number (also four times vertically).
    The goal is to have no missing values (as in fact there are no missing values).

    Due to my randomization, I can't simply expand (or at least I could not find a way).

    Here is a sample of 100 observations using the dataex command, hopefully anyone can help me with this.

    Perhaps, to explain the first two treatments. The first individual faced treatments 1-4 in order and I would like to have 1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4 as either treatment or a new variable.
    The second participant faced 1,3,2,4 and I would like that to be 1,1,1,1,3,3,3,3,2,2,2,2,4,4,4,4 without any missing variables.
    There are always 12 missing values between the participants.

    Thank you very much!

    Code:
     dataex treatment
    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte treatment
    1
    2
    3
    4
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    1
    3
    2
    4
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    3
    4
    1
    2
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    4
    2
    3
    1
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    2
    4
    3
    1
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    1
    3
    4
    2
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    1
    3
    2
    4
    end
    ------------------ copy up to and including the previous line ------------------

  • #2
    Thank you for using -dataex- on your very first post.

    Code:
    gen group = ceil(_n/16)
    sort group, stable
    by group: gen re_cycle = ceil(_n/4)
    by group: gen new_treatment = treatment[re_cycle]

    Comment


    • #3
      Dear Clyde, Thank you so very much! All works great now! I much appreciate your help!

      Comment

      Working...
      X