Announcement

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

  • Error in a code with conditionals

    Hi!!

    Could anyone please tell me what is wrong with this code

    gen trans_auxi_sexta_pri= 0
    replace trans_auxi_sexta_pri= sub_beca_din/1 if frecuencia_dinero==1 | sub_beca_din/2 if frecuencia_dinero==2 | sub_beca_din/6 if frecuencia_dinero==3 | sub_beca_din/12 if frecuencia_dinero==4

    Thanks!

  • #2
    replace trans_auxi_sexta_pri= sub_beca_din/1 if frecuencia_dinero==1 | sub_beca_din/2 if frecuencia_dinero==2 | sub_beca_din/6 if frecuencia_dinero==3 | sub_beca_din/12 if frecuencia_dinero==4
    isn't even close to legal Stata syntax.

    I'm not 100% sure what you are hoping to calculate here, but my best guess is that you want:
    Code:
    gen denominator = 1 if frecuencia_dinero == 1
    replace denominator = 2 if frecuencia_dinero == 2
    replace denominator = 6 if frequencia_dinero == 3
    replace denominator = 12 if frequencia_dinero == 4
    gen trans_auxi_sexta_pri = sub_beca_din/denominator
    replace trans_auxi_sexta_pri = 0 if missing(denominator)
    You can't chain multiple -if- qualifiers in a single Stata command. What you wrote sounds fine when read aloud as an English sentence, but Stata is not English and you have to adhere to the rules of Stata syntax, which are much more restrictive. (You can reduce the four commands creating the denominator variable into a single command by nesting -cond()- functions in a single -if- qualifier. But it is difficult to write that code correctly, and it is difficult to read and understand once it is written. So I don't recommend trying.)

    Comment


    • #3
      Code:
       gen denominator = real(word("1 2 6 12", frecuencia_dinero))
      and

      Code:
       gen denominator = cond(frecuencia_dinero == 1, 1, ///                  
                         cond(frecuencia_dinero == 2, 2, ///                  
                         cond(frecuencia_dinero == 3, 6, ///                  
                         cond(frecuencia_dinero == 4, 12, .))))
      are examples of one-line statements. I am tempted to use cond() more frequently than Clyde Schechter but I tend to agree that the more long-winded code will be found easiest to follow by most readers, including yourself in future.
      Last edited by Nick Cox; 05 Aug 2023, 10:32.

      Comment


      • #4
        Understood! Thank you very much Clyde

        Comment


        • #5
          Although the first suggestion in #3 is elegant, I don't see a difference in using
          Code:
          recode frecuencia_dinero (1=1) (2=2) (3=6) (4=12) (else=.), gen(denominator)
          Here you could easily modify it to include something like 5/max:
          Code:
          recode frecuencia_dinero (1=1) (2=2) (3=6) (4=12) (5/max=60) (else=.), gen(denominator)

          Comment


          • #6
            Dirk Enzmann Good suggestion. I've always been a little averse to recode, but the reasons aren't compelling for anyone used to it.

            Comment


            • #7
              Thank you guys!!

              Comment

              Working...
              X