Announcement

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

  • Creating new var w/ multiple values of another var

    Hello, I have two variables, IS and Code. I would like IS to take on different values depending on what Code is. For example, I would like IS to equal 10401 if Code = 10085, 10087, 10090, 10091, 10095, or 60031. I would like IS to equal 10402 if Code = 10094, 10164, 60027, 60029, 60032, 60033 or 60034.

    In this way I am trying to map about 900 Code values to about 100 IS values. Is there a relatively quick way I can do this using loops and groups instead of doing replace IS over and over again? I am very new to Stata and do not have much experience.

  • #2
    Welcome to the Stata Forum / Statalist,

    I fail to envisage a place for a loop here.

    If I understood right, it seems you need to use - replace if -, for you have specific values to replace.
    Best regards,

    Marcos

    Comment


    • #3
      Thank you for responding.

      Yes I am using -replace if- a lot (see below). I am just wondering if there is an easier way.

      gen IS = 10401 if Code==10085
      replace IS = 10401 if Code==10087
      replace IS = 10401 if Code==10090
      replace IS = 10401 if Code==10091
      replace IS = 10401 if Code==10095
      replace IS = 10401 if Code==60031

      replace IS = 10402 if Code==10094
      replace IS = 10402 if Code==10164
      replace IS = 10402 if Code==60027
      replace IS = 10402 if Code==60029
      replace IS = 10402 if Code==60032
      replace IS = 10402 if Code==60033
      replace IS = 10402 if Code==60034
      replace IS = 10402 if Code==60035
      replace IS = 10402 if Code==60100

      replace IS = 10801 if Code==10134
      replace IS = 10801 if Code==10136
      replace IS = 10801 if Code==10143
      replace IS = 10801 if Code==60072
      replace IS = 10801 if Code==60104
      replace IS = 10801 if Code==60209

      replace IS = 10802 if Code==10059
      replace IS = 10802 if Code==10132
      replace IS = 10802 if Code==10138
      replace IS = 10802 if Code==10148
      replace IS = 10802 if Code==60063
      replace IS = 10802 if Code==60064
      replace IS = 10802 if Code==60073
      replace IS = 10802 if Code==86258

      Comment


      • #4
        The merge command may be the best way of attacking this problem. Here's an example based on your data.
        Code:
        // invent up example data
        clear
        input float Code IS
        10085 10401 
        10087 10401 
        10090 10401 
        10091 10401 
        10095 10401 
        60031 10401 
        10094 10402 
        10164 10402 
        60027 10402 
        60029 10402 
        60032 10402 
        60033 10402 
        60034 10402 
        60035 10402 
        60100 10402 
        end
        tempfile IS
        save `IS'
        
        clear
        input float(id Code)
        101 10085
        102 10087
        103 10090
        104 10091
        105 10094
        106 10095
        107 10090
        108 60027
        109 60029
        110 60031
        111 60032
        112 60035
        113 60100
        114 66666
        end
        tempfile master
        save `master'
        
        // demonstrate technique
        use `master', clear
        merge m:1 Code using `IS'
        
        list, clean
        
        drop if _merge==2
        drop _merge
        sort id
        
        list, clean
        Code:
        . // demonstrate technique
        . use `master', clear
        
        . merge m:1 Code using `IS'
        
            Result                           # of obs.
            -----------------------------------------
            not matched                             4
                from master                         1  (_merge==1)
                from using                          3  (_merge==2)
        
            matched                                13  (_merge==3)
            -----------------------------------------
        
        . 
        . list, clean
        
                id    Code      IS            _merge  
          1.   101   10085   10401       matched (3)  
          2.   102   10087   10401       matched (3)  
          3.   103   10090   10401       matched (3)  
          4.   107   10090   10401       matched (3)  
          5.   104   10091   10401       matched (3)  
          6.   105   10094   10402       matched (3)  
          7.   106   10095   10401       matched (3)  
          8.   108   60027   10402       matched (3)  
          9.   109   60029   10402       matched (3)  
         10.   110   60031   10401       matched (3)  
         11.   111   60032   10402       matched (3)  
         12.   112   60035   10402       matched (3)  
         13.   113   60100   10402       matched (3)  
         14.   114   66666       .   master only (1)  
         15.     .   10164   10402    using only (2)  
         16.     .   60033   10402    using only (2)  
         17.     .   60034   10402    using only (2)  
        
        . 
        . drop if _merge==2
        (3 observations deleted)
        
        . drop _merge
        
        . sort id
        
        . 
        . list, clean
        
                id    Code      IS  
          1.   101   10085   10401  
          2.   102   10087   10401  
          3.   103   10090   10401  
          4.   104   10091   10401  
          5.   105   10094   10402  
          6.   106   10095   10401  
          7.   107   10090   10401  
          8.   108   60027   10402  
          9.   109   60029   10402  
         10.   110   60031   10401  
         11.   111   60032   10402  
         12.   112   60035   10402  
         13.   113   60100   10402  
         14.   114   66666       .

        Comment

        Working...
        X