Announcement

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

  • Recode/Replace several variables with differing values into one variable

    I am translating the following SPSS code into STATA. The SPSS code basically recodes 3 variables with different values into one variable.

    SPSS code
    Code:
    RECODE   BB72   (1=42)  (2=35)  (3=28)  (4=21)  (5=14)  (6=7)  INTO  f_roespread .
    RECODE   BB73   (1=5.5)  (2=3.5)  (3=1.5)  INTO f_roespread .
    RECODE   BB74   (1=0.75)  (2=0.5)  (3=0.25)  (4=0)  INTO  f_roespread .
    EXECUTE.
    My STATA code looks much more clunky.
    Code:
    recode BB72   (1=42)  (2=35)  (3=28)  (4=21)  (5=14)  (6=7), gen(f_roespread)
    replace f_roespread=5.5 if BB73==1
    replace f_roespread=3.5 if BB73==2
    replace f_roespread=1.5 if BB73==3 
    replace f_roespread=0.75 if BB74==1
    replace f_roespread=0.5 if BB74==2
    replace f_roespread=0.25 if BB74==3
    replace f_roespread=0 if BB74==4
    Is there a more elegant way to do this in STATA?

    Many thanks, Nina

  • #2
    I do not use SPSS, but it is difficult to understand the structure of your data as the replace statements may be overwriting previously recoded values. Show us your variables in Stata.

    Code:
    dataex BB72 BB73 BB74

    Comment


    • #3
      Thanks, and apologies for lack of clarity.

      So the participants filled out one of either BB72, BB73, BB74 and these are being made into one variable f_roespread. i.e. only one of the three BB variables is <.

      So recodng Amount of roespread per day(BB72) or per week(BB73) or per month (BB74) in categories to frequency of f_roespread - times per week numeric.

      Code:
      * Example generated by -dataex-. 
      input double(BB72 BB73 BB74 f_roespread)
      . . 2  .5
      . . 4   0
      6 . .   7
      . . .   0
      . 2 . 3.5
      . . 3 .25
      . . 3 .25
      . . 4   0
      . . 4   0
      6 . .   7
      . . 4   0
      . . 4   0

      Comment


      • #4
        Thanks. This should do it:

        Code:
        recode BB72   (1=42)  (2=35)  (3=28)  (4=21)  (5=14)  (6=7), gen(f_roespread1)
        recode BB73   (1=5.5)  (2=3.5)  (3=1.5), gen(f_roespread2)
        recode BB74   (1=0.75)  (2=0.5)  (3=0.25)  (4=0), gen(f_roespread3)
        egen f_roespread= rowmax(f_roespread?)
        drop f_roespread?
        Res.:

        Code:
        . l, sep(0)
        
             +-------------------------------+
             | BB72   BB73   BB74   f_roes~d |
             |-------------------------------|
          1. |    .      .      2         .5 |
          2. |    .      .      4          0 |
          3. |    6      .      .          7 |
          4. |    .      .      .          . |
          5. |    .      2      .        3.5 |
          6. |    .      .      3        .25 |
          7. |    .      .      3        .25 |
          8. |    .      .      4          0 |
          9. |    .      .      4          0 |
         10. |    6      .      .          7 |
         11. |    .      .      4          0 |
         12. |    .      .      4          0 |
             +-------------------------------+
        
        .

        Comment


        • #5
          Code:
          generate f_roespread = (7-BB72)*7
          replace  f_roespread = (1-BB73)*2+5.5 if mi(f_roespread)
          replace  f_roespread = (4-BB74)*0.25  if mi(f_roespread)
          Last edited by daniel klein; 11 Sep 2023, 05:58. Reason: got BB73 wrong way round

          Comment


          • #6
            Thank you both Andrew and Daniel.

            Both neat solutions, and I got to introduced to the wildcard ? and mi(var) - both of which will also help improve my Stata coding.

            Comment


            • #7
              Note that both #4 and #5 will return missing values for observations with missing values on all three variables (obs. 4 in the example data). I cannot speak for Andrew but I find this much more plausible than assigning a value of 0 as in the original example in #3.

              Comment


              • #8
                Originally posted by daniel klein View Post
                I cannot speak for Andrew but I find this much more plausible than assigning a value of 0 as in the original example
                I agree, especially since zeros represent some different conditions in this setting.

                Comment

                Working...
                X