Announcement

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

  • Creating a dummy variable that checks if the substring of common ids is the sa e

    Hi
    I need to clean data in doing so I need to create a dummy variable. I want to see if for common ids the first 4 letters of the zip are similar or not
    here a small example what I want to let the dummy do:
    id zip dummy ..
    1 12345 1
    1 12344 1
    2 12332 0
    2 12342 0
    I would be super nice if somebody could help me.
    Best,
    JB

  • #2
    Julian:
    welcome to this forum.
    You may want to try:
    Code:
    input id zip
    1 12345   
    1 12344    
    2 12332    
    2 12342
    end
    
    . tostring zip, g(string_zip)
    
    . gen wanted=substr(string_zip,1,4)
    
    . bysort id: gen dummy=1 if wanted[_N]== wanted[_N-1]
    
    
    . bysort id: replace dummy=0 if wanted[_N]!= wanted[_N-1]
    
    
    . list
    
         +----------------------------------------+
         | id     zip   string~p   wanted   dummy |
         |----------------------------------------|
      1. |  1   12345      12345     1234       1 |
      2. |  1   12344      12344     1234       1 |
      3. |  2   12332      12332     1233       0 |
      4. |  2   12342      12342     1234       0 |
         +----------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment

    Working...
    X