Announcement

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

  • reshape command

    Hi stata user,

    I have facing some problem while using reshape wide command, here is my data

    . dataex

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id str3(elligible diabetic) str2 cholestrol str3 stroke
    1 "Yes" "No" "No" "Yes"
    2 "Yes" "Yes" "No" "No" 
    3 "No" "Yes" "Yes" "No" 
    4 "No" "No" "No" "No" 
    4 "Yes" "Yes" "No" "No" 
    5 "Yes" "No" "No" "No" 
    6 "Yes" "Yes" "Yes" "Yes"
    7 "Yes" "No" "No" "Yes"
    5 "Yes" "Yes" "No" "Yes"
    end
    ------------------ copy up to and including the previous line ------------------


    there is a duplicity in ID, I want unique id, so I tried reshape command which are as
    Code:
    bysort id:gen ab=_n
    
    . 
    . replace ab=0 if ab==.
    (0 real changes made)
    
    . reshape wide elligible diabetic cholestrol stroke, i(id) j(ab)
    (note: j = 1 2)
    
    Data                               long   ->   wide
    -----------------------------------------------------------------------------
    Number of obs.                        9   ->       7
    Number of variables                   6   ->       9
    j variable (2 values)                ab   ->   (dropped)
    xij variables:
                                  elligible   ->   elligible1 elligible2
                                   diabetic   ->   diabetic1 diabetic2
                                 cholestrol   ->   cholestrol1 cholestrol2
                                     stroke   ->   stroke1 stroke2
    -----------------------------------------------------------------------------
    end
    now, variables converted into 6 to 9,

    now there are 2 variable for one thing.

    If I do this, output looks like:

    Code:
    gen elligible =  elligible1+ elligible2
    
    . tab elligible
    
      elligible |      Freq.     Percent        
    ------------+-----------------------------------
             No |          2       22.22 
            Yes |          5       55.55      
      YesYes |          2       22.22      
    ------------+-----------------------------------
          Total |          9      100.00
    end
    please suggest what to do to convert into single variable without duplicity of ID.

  • #2
    I don't follow what you want that is different from what you've done. But here is a small amount of extra technique, with incidental correction of typos.

    tabm is from the tab_chi package on SSC, and must be installed before you can use it.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id str3(eligible diabetic) str2 cholesterol str3 stroke
    1 "Yes" "No" "No" "Yes"
    2 "Yes" "Yes" "No" "No" 
    3 "No" "Yes" "Yes" "No" 
    4 "No" "No" "No" "No" 
    4 "Yes" "Yes" "No" "No" 
    5 "Yes" "No" "No" "No" 
    6 "Yes" "Yes" "Yes" "Yes"
    7 "Yes" "No" "No" "Yes"
    5 "Yes" "Yes" "No" "Yes"
    end
    
    bysort id : gen ab = _n 
    
    reshape wide eligible-stroke, i(id) j(ab)
    
    egen eligible = concat(eligible?), p(" ")
    
    tab eligible 
    
    tabm eligible?
    Code:
    
    
    . tab eligible 
    
       eligible |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             No |          1       14.29       14.29
         No Yes |          1       14.29       28.57
            Yes |          4       57.14       85.71
        Yes Yes |          1       14.29      100.00
    ------------+-----------------------------------
          Total |          7      100.00
    
    . 
    . tabm eligible? 
    
               |        values
      variable |        No        Yes |     Total
    -----------+----------------------+----------
    1 eligible |         2          5 |         7 
    2 eligible |         0          2 |         2 
    -----------+----------------------+----------
         Total |         2          7 |         9 

    Comment

    Working...
    X