Announcement

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

  • How to generate a variable efficiently (avoid repetitive replacements)?

    Hello to all,

    I would like to know if there is a more efficient way to generate this code, in order to save time and avoid copy and paste errors.


    Would there be a suggestion to generate this in a simpler and more efficient way, please?

    Code:
    gen activites_now="En études" if weistu==1 &  staaus1==1 & k_erwerbstat==3
    replace activites_now="En études" if weistu==1 &  staaus2==1 & k_erwerbstat==3
    replace activites_now="En études" if weistu==1 &  staaus3==1 & k_erwerbstat==3
    replace activites_now="En études" if weistu==1 &  staaus4==1 & k_erwerbstat==3
    replace activites_now="En études" if weistu==1 &  staaus5==1 & k_erwerbstat==3
    
    replace activites_now="En études" if weinach==1 &  stanuh==1 & k_erwerbstat==3
    replace activites_now="En études" if weinach==1 &  stanfh==1 & k_erwerbstat==3
    replace activites_now="En études" if weinach==1 &  stanph==1 & k_erwerbstat==3
    
    
    replace activites_now="En études" if weiaus==1 &  starz==1 & k_erwerbstat==3
    replace activites_now="En études" if weiaus==1 &  strech==1 & k_erwerbstat==3
    replace activites_now="En études" if weiaus==1 &  stpsy==1 & k_erwerbstat==3
    replace activites_now="En études" if weiaus==1 &  stvik==1 & k_erwerbstat==3
    replace activites_now="En études" if weiaus==1 &  stvik==1 & k_erwerbstat==3
    replace activites_now="En études" if weiaus==1 &  stberb13==1 & k_erwerbstat==3
    replace activites_now="En études" if weiaus==1 &  stberi==1 & k_erwerbstat==3
    replace activites_now="En études" if weiaus==1 &  stunt==1 & k_erwerbstat==3
    replace activites_now="En études" if weiaus==1 &  stsons==1 & k_erwerbstat==3
    
    replace activites_now="En études" if weipdoc==1 &  sthab==1 & k_erwerbstat==3
    replace activites_now="En études" if weipdoc==1 &  stpdoc==1 & k_erwerbstat==3
    
    replace activites_now="En études et emploi" if weistu==1 &  staaus1==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weistu==1 &  staaus2==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weistu==1 &  staaus3==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weistu==1 &  staaus4==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weistu==1 &  staaus5==1 & k_erwerbstat==1
    
    replace activites_now="En études et emploi" if weinach==1 &  stanuh==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weinach==1 &  stanfh==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weinach==1 &  stanph==1 & k_erwerbstat==1
    
    
    replace activites_now="En études et emploi" if weiaus==1 &  starz==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weiaus==1 &  strech==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weiaus==1 &  stpsy==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weiaus==1 &  stvik==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weiaus==1 &  stvik==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weiaus==1 &  stberb13==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weiaus==1 &  stberi==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weiaus==1 &  stunt==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weiaus==1 &  stsons==1 & k_erwerbstat==1
    
    replace activites_now="En études et emploi" if weipdoc==1 &  sthab==1 & k_erwerbstat==1
    replace activites_now="En études et emploi" if weipdoc==1 &  stpdoc==1 & k_erwerbstat==1
    
    . . .
    Thank you very much in advance,

    Michael

  • #2
    Each block of your commands can be reduced to a single line using the inlist function. For example,

    Code:
    replace activites_now="En études" if weiaus==1 &  k_erwerbstat==3 & inlist(1, starz, strech, stpsy, stvik, stberb13, stberi, stunt, stsons)

    Comment


    • #3
      Some looping or macro variable can be used. Here are some examples:

      Looping through numeric index:

      Original:
      Code:
      replace activites_now="En études" if weistu==1 &  staaus2==1 & k_erwerbstat==3
      replace activites_now="En études" if weistu==1 &  staaus3==1 & k_erwerbstat==3
      replace activites_now="En études" if weistu==1 &  staaus4==1 & k_erwerbstat==3
      replace activites_now="En études" if weistu==1 &  staaus5==1 & k_erwerbstat==3
      The only thing that changes is the number after staaus, so we can compress it into:

      Code:
      forvalues x = 2/5{
          replace activites_now="En études" if weistu==1 &  staaus`x'==1 & k_erwerbstat==3
      }
      Looping through variables:

      Original:
      Code:
      replace activites_now="En études" if weiaus==1 &  starz    ==1 & k_erwerbstat==3
      replace activites_now="En études" if weiaus==1 &  strech   ==1 & k_erwerbstat==3
      replace activites_now="En études" if weiaus==1 &  stpsy    ==1 & k_erwerbstat==3
      replace activites_now="En études" if weiaus==1 &  stvik    ==1 & k_erwerbstat==3
      replace activites_now="En études" if weiaus==1 &  stvik    ==1 & k_erwerbstat==3  // <--- NOTICE: This is duplicated
      replace activites_now="En études" if weiaus==1 &  stberb13 ==1 & k_erwerbstat==3
      replace activites_now="En études" if weiaus==1 &  stberi   ==1 & k_erwerbstat==3
      replace activites_now="En études" if weiaus==1 &  stunt    ==1 & k_erwerbstat==3
      replace activites_now="En études" if weiaus==1 &  stsons   ==1 & k_erwerbstat==3
      This one is a bit different, the only thing changes is the variable in the middle condition. We can loop through the variables instead:

      Code:
      foreach x in starz strech stpsy stvik stberb13 stberi stunt stsons{
          replace activites_now="En études" if weiaus==1 & `x'==1 & k_erwerbstat==3
      }

      Comment


      • #4
        Hello Hemanshu Kumar.

        Thanks again for your (quick) answer!

        Michael

        Comment


        • #5
          Thanks Ken Chui for your answer,

          Michael

          Comment


          • #6
            I have just a small question about these two methods, Hemanshu Kumar and Ken Chui:
            • Is it better to use a loop and macro, or rather inlist? Or is it more a matter of taste?
            Thanks.

            Michael

            Comment


            • #7
              Originally posted by Michael Duarte Goncalves View Post
              I have just a small question about these two methods, Hemanshu Kumar and Ken Chui:
              • Is it better to use a loop and macro, or rather inlist? Or is it more a matter of taste?
              Thanks.

              Michael
              I tried to run something like this to test the time:

              Code:
              clear
              set obs 100000000
              foreach x in 1 2 3 4 5{
                  gen y_`x' = floor(runiform()*5)+1
              }
              
              gen wanted1 = .
              display "$S_TIME"
              replace wanted1 = 1 if y_1 == 1 & inlist(1, y_2, y_3, y_4, y_5)
              display "$S_TIME"
              
              gen wanted2 = .
              display "$S_TIME"
              forvalues x = 2/5{
              replace wanted2 = 1 if y_1 == 1 & y_`x' == 1
              }
              display "$S_TIME"
              The inlist method took about 6 seconds on my machine. And the forvalues loop took about 14 seconds.

              So, from what I could gather, inlist seems faster. But the speed probably only matters if you have a huge data set.
              Last edited by Ken Chui; 28 Nov 2022, 06:19.

              Comment


              • #8
                Ken Chui : Thanks for the clarifying and excellent explanation

                Comment

                Working...
                X