Announcement

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

  • Help with replace and egen

    Hi everyone,

    I have the dataset below. How can i replace the S3_Q10K with value in S1C_Q0_*? For example if in S3_Q10K is = 1 then i want to replace it with value in S1C_Q0_1, if in S3_Q10K is = 2 then i want to replace it with value in S1C_Q0_2, if in S3_Q10K is = 4 then i want to replace it with value in S1C_Q0_4 and so on.

    i used

    Code:
     replace S3_Q1K = S1C_Q0_1 if S3_Q1K ==1

    which worked but i am always told to use short codes thus i didnt want to use the above code 19 times and i was not sure how to use loop code

    Thank you so much for your help.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(S3_Q10K S3_Q3P S1C_Q0_1 S1C_Q0_2 S1C_Q0_3 S1C_Q0_4 S1C_Q0_5 S1C_Q0_6)
     1  . 1 0 . . . .
    77  . 1 0 1 . . .
     1  . 1 0 . . . .
     1  . 1 0 1 1 0 .
     2  1 1 1 1 1 0 0
     1  1 1 0 1 0 1 0
     7 88 1 1 1 0 1 0
     3  1 1 0 1 . . .
     5 88 1 0 1 0 1 0
     6 88 1 0 0 0 1 1
     1  1 0 1 1 0 0 .
     2  . 1 0 1 1 1 1
     2  . 1 0 . . . .
     3  1 1 0 1 0 . .
     3  1 1 0 1 0 0 1
     1  . 1 0 0 0 1 .
     1  . 1 0 . . . .
     9  . 0 0 0 1 0 1
     1  . 1 1 0 1 0 0
     5  . 1 0 1 0 1 0
     1  1 1 0 1 0 1 1
     3  . 1 0 0 1 1 1
     2 88 1 0 1 1 . .
     2  . 0 1 0 0 0 .
     6 88 1 0 0 0 0 1
     2  2 1 0 0 1 . .
     1  . 1 0 1 1 1 0
     6  1 0 1 1 1 1 0
     3  . 1 0 1 0 0 1
     1  . 1 0 0 0 1 1
    44 88 1 0 . . . .
     1  . 1 0 1 . . .
     6 88 1 0 0 1 1 1
     1  . 1 0 0 0 1 .
     1  . 1 0 1 0 0 .
     2  . 1 0 . . . .
     3  . 0 0 1 . . .
     3  . 0 1 1 1 1 0
     1  . 1 0 0 1 . .
     6  6 1 0 0 0 1 1
     1  . 1 0 0 0 1 .
     1  . 1 0 . . . .
     2  . 0 0 1 0 1 .
     1  . 0 1 1 1 0 0
     1  . 1 0 0 1 1 .
     .  3 1 1 1 0 0 1
     2  1 0 1 0 . . .
     1  . 1 0 0 1 . .
     7  1 1 0 1 0 1 0
     1  . 1 0 1 . . .
     2  . 0 0 1 1 0 .
    88  . 1 0 . . . .
     2  2 0 0 1 1 0 .
     4  1 1 0 0 1 . .
     2  . 1 0 1 . . .
     2  . 1 0 . . . .
     1  . 1 0 0 1 1 .
     1  . 1 0 1 0 1 0
     1  . 0 0 0 1 1 .
     2  1 1 0 . . . .
     2  . 1 0 0 . . .
     3  3 1 0 0 1 0 0
     3  . 1 0 1 1 0 0
     2  . 1 0 . . . .
     1  . 1 0 1 0 1 1
     2  . 1 0 0 1 0 0
     4  4 1 0 0 1 . .
     1  . 1 0 1 0 0 0
     5  . 1 0 0 0 1 1
     2 88 1 0 1 1 1 0
     1  . 1 0 1 0 1 .
     3  . 1 0 1 1 1 .
     1  . 1 0 . . . .
    88  . 1 1 0 0 1 .
     1  . 1 0 . . . .
     1  1 1 0 0 1 . .
     1  . 1 0 1 1 0 0
     2  . 1 0 . . . .
    11  . 1 0 1 0 1 0
     3  2 0 1 1 1 0 0
     3  . 1 0 1 0 . .
     2  . 1 0 . . . .
     1  . 1 0 1 0 . .
     3  . 1 0 1 . . .
     3  . 1 1 1 1 0 0
     4  . 1 0 1 0 . .
     3  . 1 0 1 . . .
     1  . 1 0 1 1 . .
     1  . 1 0 1 1 . .
    88 88 1 0 0 1 1 .
    88  1 1 0 1 1 1 .
    88  . 0 1 1 0 0 .
    88  . 1 0 1 . . .
     2  . 1 0 1 1 0 .
    88  . 1 0 1 . . .
    88  . 1 0 1 0 . .
    88  . 1 0 1 1 0 0
    88  . 0 1 0 1 . .
    77  . 1 0 . . . .
    88  . 1 0 1 0 . .
    end
    Last edited by Naika Sangroo; 03 Aug 2020, 18:04.

  • #2
    I was able to figure it out myself sorry for occupying the space or taking anyone's valuable time. :-) I used

    Code:
    forval i= 1/6{
         replace S3_Q1K = S1C_Q0_`i' if S3_Q1K ==`i'
         replace S3_Q3P = S1C_Q0_`i' if S3_Q1K ==`i'
    }

    Comment


    • #3
      I gather this code is short enough. Besides, a shorter code won’t be necessarily clearer.

      That said, this thread may be helpful: https://www.statalist.org/forums/for...-each-variable
      Best regards,

      Marcos

      Comment


      • #4
        Thank you that is very helpful thread.

        Comment

        Working...
        X