Announcement

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

  • Reshape command

    Hello.

    I have a dataset with 4 dummy variables (medical examinations chooses = 1 or not = 0). 2 of the examinations are invasive. I have to check whether the invasive nature affected their choices. So, I have to use the reshape command. What I should do?
    Last edited by Joshua Payn; 08 Nov 2022, 12:22. Reason: reshape

  • #2
    It is rare that a question requesting help with code can be answered without example data. And I would say that when -reshape- is involved, it goes from rare to pretty much never. Please post back showing example data, using the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    It would probably be a good idea, to avoid confusion, if you also mock up a little display of what you want the end result to look like.

    Comment


    • #3
      . dataex

      ----------------------- copy starting from the next line -----------------------
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(post_bmi post_bp post_dia post_cho) byte(pre_bmi pre_bp pre_dia pre_cho) float(any_health_prob has_PCP uninsured age black_dr HSless low_income)
      1 1 1 1 1 1 1 1 1 0 0 35 0 0 0
      1 1 1 1 1 1 1 1 1 . 0 18 0 1 0
      1 1 1 0 1 1 1 0 0 1 0 57 0 1 1
      1 1 0 0 1 1 0 0 0 1 1 19 0 1 1
      1 1 1 1 1 1 1 1 1 1 0 34 1 1 0
      1 0 0 0 1 0 0 0 1 1 . 50 0 . 1
      1 1 1 1 1 1 1 1 0 0 0 44 1 1 .
      0 1 0 0 0 1 0 0 1 0 0 55 1 1 1
      1 1 0 1 0 1 0 1 0 1 0 60 0 1 0
      1 1 1 1 0 1 1 0 1 1 0 51 1 . 1
      0 0 0 0 0 0 0 0 1 1 0 65 0 0 0
      0 1 1 1 0 1 1 1 1 1 0 67 0 0 0
      1 1 1 1 1 1 0 0 1 1 1 42 0 1 1
      1 1 1 1 1 1 1 1 1 1 0 44 0 0 0
      0 1 0 0 0 1 0 0 1 1 0 59 1 1 0
      0 0 0 0 0 0 0 0 0 1 0 30 1 0 0
      0 0 0 1 0 1 1 1 1 1 0 63 0 1 1
      1 1 1 1 1 1 1 1 0 1 1 24 0 0 1
      1 1 1 1 1 1 1 1 1 1 0 63 1 0 0
      0 1 0 0 0 0 0 0 1 1 0 50 1 1 1
      1 1 1 1 1 1 1 1 . . .  . 0 . .
      1 1 1 1 1 1 1 1 0 1 . 39 1 . 1
      0 0 0 0 0 0 0 0 1 1 0 46 1 0 0
      1 0 0 0 0 0 0 0 1 0 1 23 1 0 0
      1 1 1 1 0 0 1 1 0 0 . 45 1 . 1
      1 1 1 1 1 1 1 1 1 0 0 23 1 0 0
      0 0 0 0 0 0 0 0 1 1 0 65 0 1 1
      1 1 1 1 1 1 0 0 1 . . 28 1 . 1
      1 1 1 0 1 1 0 0 1 . 1 37 0 1 1
      0 1 1 1 0 1 1 1 1 1 1 58 1 1 0
      1 1 1 1 1 1 1 1 1 . 0 46 1 1 1
      1 1 1 1 1 1 1 1 1 1 0 37 1 1 0
      0 0 0 0 0 0 0 0 1 0 1 40 0 0 0
      1 1 1 1 0 0 0 0 0 0 . 52 1 . .
      0 1 0 1 0 1 0 1 0 0 0 45 1 0 1
      0 1 0 0 0 1 0 0 0 0 0 59 0 . .
      0 0 0 0 0 0 0 0 1 0 . 57 0 1 1
      1 1 0 0 1 0 0 0 0 0 0 26 1 0 0
      1 1 0 0 1 1 0 0 1 0 0 71 1 1 0
      1 1 0 0 0 0 0 0 0 0 1 52 0 . .
      1 1 1 1 1 1 1 1 0 1 0 38 1 0 0
      0 1 0 1 0 0 0 0 1 1 0 47 1 0 0
      1 1 1 1 0 0 0 0 1 1 0 52 1 1 1
      1 1 1 1 1 1 0 1 1 1 0 50 1 0 0
      0 0 0 1 0 0 0 1 1 0 . 40 0 1 1
      0 1 1 1 0 1 1 1 1 . 0 65 0 0 0
      1 1 1 1 1 0 0 0 0 0 0 57 1 1 1
      0 1 0 0 0 1 0 0 0 1 .  . 0 . .
      1 0 0 0 1 0 0 0 0 1 0 54 1 1 1
      1 1 1 1 0 1 0 0 1 . 1 27 1 1 1
      1 1 1 1 1 0 0 0 . . . 72 1 1 0
      1 1 1 1 0 1 0 0 1 1 0 37 1 0 0
      0 1 0 0 0 0 0 0 0 1 0 45 1 1 1
      0 1 0 0 0 1 0 0 1 . . 67 0 0 0
      1 1 1 1 1 1 1 1 0 1 0 65 1 1 0
      0 0 1 0 0 0 1 0 0 . . 47 1 0 0
      1 1 1 1 1 1 1 0 0 0 . 19 1 1 0
      1 1 0 0 0 1 0 0 0 1 0 54 1 1 1
      1 1 1 1 0 0 0 0 1 1 0 30 1 1 1
      1 1 1 1 1 1 1 1 1 0 0 23 0 0 0
      1 1 1 0 0 0 0 0 0 . 0 46 1 1 1
      1 1 1 1 1 1 0 0 0 1 0 48 1 1 0
      1 1 1 0 0 0 0 0 0 . . 64 0 1 0
      1 1 1 1 1 1 1 1 1 0 1 48 0 1 1
      1 1 0 0 1 0 0 0 0 . . 31 1 1 1
      0 0 0 0 0 0 0 0 0 1 0 64 0 0 0
      1 1 0 0 1 0 0 0 1 0 . 51 0 1 0
      0 0 0 0 0 0 0 0 0 1 0 53 0 0 0
      0 0 0 0 0 0 0 0 1 1 0 55 1 1 1
      1 1 1 1 0 1 1 1 1 0 0 53 1 1 0
      1 1 0 1 1 1 0 0 1 . 1 40 1 . 0
      0 0 0 0 0 0 0 0 1 . . 43 0 1 1
      1 1 1 1 0 1 1 1 1 1 0 60 0 0 1
      1 1 0 0 0 0 0 0 0 1 0 19 1 1 0
      1 1 1 1 1 0 0 0 1 . . 85 1 . 1
      1 1 1 1 1 1 0 0 0 . . 19 0 1 1
      1 1 1 1 0 0 1 1 1 1 1 60 1 1 1
      0 1 1 0 0 1 1 0 1 . 1 69 0 1 1
      1 1 1 1 1 1 1 0 1 . . 46 0 1 1
      0 1 0 0 0 0 0 0 0 0 1 44 0 1 1
      1 1 1 1 1 1 1 1 1 1 0 63 1 1 0
      1 1 1 1 0 0 1 1 1 1 1 59 0 1 0
      1 1 0 0 0 1 0 0 0 1 0 22 1 1 1
      1 1 1 1 1 1 1 1 1 0 0 62 1 1 0
      0 0 0 0 0 0 0 1 0 1 0 47 0 1 .
      0 0 0 0 0 0 0 0 0 0 . 46 1 . .
      1 1 1 1 1 1 1 1 1 1 0 60 0 0 0
      1 1 1 1 1 1 1 1 1 . 1 47 0 1 1
      0 0 0 0 0 0 0 0 1 1 0 20 0 0 0
      0 1 1 1 0 1 1 1 1 1 0 57 0 . 0
      1 1 0 0 0 0 0 0 1 1 1 55 1 0 1
      0 0 0 0 0 0 0 0 1 . . 25 0 1 1
      1 1 0 0 1 1 0 0 1 0 0 25 0 1 1
      0 1 0 0 0 1 0 0 1 1 1 62 0 1 0
      1 1 1 1 1 1 1 1 1 1 0 63 0 0 0
      0 1 0 0 0 0 0 0 1 1 0 26 1 1 1
      0 1 1 1 0 1 1 1 1 1 . 72 0 . .
      0 1 0 1 0 1 0 1 1 1 1 53 0 1 1
      1 1 0 0 0 0 0 0 1 1 . 45 1 . .
      1 1 1 1 0 0 0 0 1 0 1 60 0 1 1
      end
      ------------------ copy up to and including the previous line ------------------

      Listed 100 out of 500 observations
      Use the count() option to list more

      .

      Comment


      • #4
        i have to check for the post phase only

        Comment


        • #5
          Yis = a + bBlacki + dInvasives + ei

          Comment


          • #6
            Code:
            gen long obs_no = _n
            ds post_*
            local stubs `r(varlist)'
            local stubs: subinstr local stubs "post" "@", all
            reshape long `stubs', i(obs_no) j(pre_post) string
            rename _* *
            The little dance with local macro stubs is perhaps overkill for this problem if the only variables involved really are bmi, bp, dia, and cho, as one could just write it all out by hand in the -reshape- command. But this approach will work even if there are a large number of pre_* and post_* variables. It does assume, however, that every pre_* variable does have a corresponding post_* variable. If there is a pre_* variable without a corresponding post_* one, it will just leave that pre_* variable as is.

            Added, as your ultimate goal is to do a regression and you might want to involve the pre_post variable in that, you may want to follow the above code with
            Code:
            encode pre_post, gen(prepost)
            drop pre_post
            and then use the numeric variable prepost instead.

            Comment

            Working...
            X