Announcement

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

  • Reshape wide for single variable; error- variable not found

    Data example below: each person has multiple rows, for various levels of the viral strain and visit. i wanted to reshape the Visit variable from long to wide but stata brings up the error:

    variable Visit not found

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float person byte strain_type double Visit float highrisk str12 infection float firstinfdate
    1  6 1 0 "No infection" .
    1  6 2 0 "No infection" .
    1  6 4 0 "No infection" .
    1 11 1 0 "No infection" .
    1 11 2 0 "No infection" .
    1 11 4 0 "No infection" .
    1 16 1 1 "No infection" .
    1 16 2 1 "No infection" .
    1 16 4 1 "No infection" .
    1 18 1 1 "No infection" .
    1 18 2 1 "No infection" .
    1 18 4 1 "No infection" .
    1 31 1 1 "No infection" .
    1 31 2 1 "No infection" .
    1 31 4 1 "No infection" .
    1 33 1 1 "No infection" .
    1 33 2 1 "No infection" .
    1 33 4 1 "No infection" .
    1 34 1 0 "No infection" .
    1 34 2 0 "No infection" .
    1 34 4 0 "No infection" .
    1 35 1 1 "No infection" .
    1 35 2 1 "No infection" .
    1 35 4 1 "No infection" .
    1 39 1 1 "No infection" .
    1 39 2 1 "No infection" .
    1 39 4 1 "No infection" .
    1 40 1 0 "No infection" .
    1 40 2 0 "No infection" .
    1 40 4 0 "No infection" .
    1 42 1 0 "No infection" .
    1 42 2 0 "No infection" .
    1 42 4 0 "No infection" .
    1 43 1 0 "No infection" .
    1 43 2 0 "No infection" .
    1 43 4 0 "No infection" .
    1 44 1 0 "No infection" .
    1 44 2 0 "No infection" .
    1 44 4 0 "No infection" .
    1 45 1 1 "No infection" .
    1 45 2 1 "No infection" .
    1 45 4 1 "No infection" .
    1 51 1 1 "No infection" .
    1 51 2 1 "No infection" .
    1 51 4 1 "No infection" .
    1 52 1 1 "No infection" .
    1 52 2 1 "No infection" .
    1 52 4 1 "No infection" .
    1 53 1 0 "No infection" .
    1 53 2 0 "No infection" .
    1 53 4 0 "No infection" .
    1 54 1 0 "No infection" .
    1 54 2 0 "No infection" .
    1 54 4 0 "No infection" .
    1 56 1 1 "No infection" .
    1 56 2 1 "No infection" .
    1 56 4 1 "No infection" .
    1 58 1 1 "No infection" .
    1 58 2 1 "No infection" .
    1 58 4 1 "No infection" .
    1 59 1 1 "No infection" .
    1 59 2 1 "No infection" .
    1 59 4 1 "No infection" .
    1 66 1 1 "No infection" .
    1 66 2 1 "No infection" .
    1 66 4 1 "No infection" .
    1 68 1 0 "No infection" .
    1 68 2 0 "No infection" .
    1 68 4 0 "No infection" .
    1 70 1 0 "No infection" .
    1 70 2 0 "No infection" .
    1 70 4 0 "No infection" .
    1 74 1 0 "No infection" .
    1 74 2 0 "No infection" .
    1 74 4 0 "No infection" .
    2  6 1 0 "No infection" .
    2 11 1 0 "No infection" .
    2 16 1 1 "No infection" .
    2 18 1 1 "No infection" .
    2 31 1 1 "No infection" .
    2 33 1 1 "No infection" .
    2 34 1 0 "No infection" .
    2 35 1 1 "No infection" .
    2 39 1 1 "No infection" .
    2 40 1 0 "No infection" .
    2 42 1 0 "No infection" .
    2 43 1 0 "No infection" .
    2 44 1 0 "No infection" .
    2 45 1 1 "No infection" .
    2 51 1 1 "No infection" .
    2 52 1 1 "No infection" .
    2 53 1 0 "No infection" .
    2 54 1 0 "No infection" .
    2 56 1 1 "No infection" .
    2 58 1 1 "No infection" .
    2 59 1 1 "No infection" .
    2 66 1 1 "No infection" .
    2 68 1 0 "No infection" .
    2 70 1 0 "No infection" .
    2 74 1 0 "No infection" .
    end
    format %td firstinfdate
    label values highrisk highrisk_
    label def highrisk_ 0 "Not High-risk", modify
    label def highrisk_ 1 "High-Risk", modify

    i used this code:
    Code:
    reshape wide Visit , i(ID strain_type) j(Visit)
    Im not sure how to debug this-am i specifying the reshape command incorrectly? should i try doing this an alternative way instead of using reshape? something like

    Code:
    gen Visit1=.
    gen Visit2=.
    gen Visit3=.
    gen Visit4=.
    gen Visit5=.
    foreach x of numlist 1/5 {
    replace Visit`x'=1 if Visit==`x';
    }
    Last edited by Sakshi Rajatbhai Tewari; 25 Jul 2022, 12:30.

  • #2
    The variable Visit cannot serve both as the variable to be widened and as the j() option.

    It is unclear to me what you want to do. The alternative code you show at the bottom of #1 will simply create a series of badly encoded indicator ("dummy") variables for visit--which, even if you do this is probably just a waste of time since there are few circumstances where indicator variables are needed in modern Stata.

    If you show what you want the results to look like, probably somebody can figure out how to get it.

    Added: Also, most Stata commands work best, or only, with the long data layout you already have. So unless you know for a fact that you will be doing some of the few things that work best with wide layout, you probably shouldn't be -reshape-ing your data anyway. So give that some thought before proceeding.
    Last edited by Clyde Schechter; 25 Jul 2022, 12:40.

    Comment


    • #3
      Youre correct- i didnt give it as much thought- just realised its not even what i wanted- i realised it after i ran the dummy variable code. I'll have to resend a data example to explain. i'll update here when i get to it next!

      Comment

      Working...
      X