Announcement

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

  • split sum up values into dummy variable by id

    hi

    Click image for larger version

Name:	s.PNG
Views:	1
Size:	1.7 KB
ID:	1735338



    the number of 142, 71, 117 ... it's not a real values, and it just a count sum of each variable.
    how can I expand values into dummy variable by id?
    I want to make 142 into 142 variable consist of 1 and 0
    Last edited by Jiyoung Hwang; 27 Nov 2023, 23:35.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(var1 var2 var3 var4 var5)
    142 71 117 119 61
    end
    
    egen biggest = rowmax(var1-var5)
    replace biggest = biggest + 1
    expand biggest
    drop biggest
    foreach v of varlist var1-var5 {
        local ones = `v'[1]
        replace `v' = 1 in 1/`ones'
        replace `v' = 0 in `=`ones'+1'/L
    }
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 18, 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.

    Comment


    • #3
      thank you so much!

      Comment

      Working...
      X