Announcement

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

  • Generate the difference between 2 variables

    Hi ,
    I have 20 variables representing an outcome measure.
    The same outcome/variable was measure twice at t1 and t2.
    My setup is wide and I use Stata 16.1:

    var1_1 var1_2 var2_1 var2-2............var20_1 var20_2

    I would like to use a loop and generate for each pair of variables the difference.
    diff_var1==var1_2 -var1_1
    diff _var2==var2_2-var2_1
    .............................
    diff_var20==var20_2-var20_1
    Your help would be greatly appreciated.
    Thank you,
    Nikos

  • #2
    Try this:

    Code:
    forvalues i=1/20 {
    gen diff_var`i'==var`i'_2 -var`i'_1
    }

    Comment


    • #3
      Thank you Joro.
      When I wrote my message I tried to simplify it but this was not a good idea. I apologise for that.
      My variables have different names and not var1_1 var1_2 ect. It is more like a_1 a_2 b_1 b_2 c_1 c_2...ect. The only constant part is the ending _1 or _2. Therefore, I would need to loop over each variable ending with _1 and each variable ending with _2 and then generate the difference for each variable diff_a diff_b ect.
      Thank you,
      Nikos

      Comment


      • #4
        Nikolaos, I have the feeling that you are simplifying again, and again we would not be getting there... You need to tell me how exactly your variables look like. The loop depends on the logic in your variables:

        Code:
        foreach l in a b c   {
        
        gen diff_`l' = `l'_2 - `l'_1
        
        }
        Originally posted by Nikolaos Pandis View Post
        Thank you Joro.
        When I wrote my message I tried to simplify it but this was not a good idea. I apologise for that.
        My variables have different names and not var1_1 var1_2 ect. It is more like a_1 a_2 b_1 b_2 c_1 c_2...ect. The only constant part is the ending _1 or _2. Therefore, I would need to loop over each variable ending with _1 and each variable ending with _2 and then generate the difference for each variable diff_a diff_b ect.
        Thank you,
        Nikos

        Comment


        • #5
          Thank you Joro,
          There are 20 variables with unique names and ending at either _1 or _2, like a_1 a_2, b_1 b_2.
          However, I do not have a variable like a , b and so on. There is always either a _1 or _2 after the string name.
          There is no logic to the var names as they pertain to various measurements in radiographs.
          Each var has observations collected at t1 and t2.
          I hope this is better.
          Thank you and best wishes,
          Nikos

          Comment


          • #6
            Hi Nikolaos,

            you may want to try this idea:
            Code:
            qui d *_2, varlist
            local t2 `r(varlist)' // capture t2 variables
            qui d *_1, varlist
            local t1 `r(varlist)' // capture t1 variables
            
            foreach i of numlist 1/`=wordcount("`t2'")' {
               //looping through each variable in the t1, t2 list
               local time2=word("`t2'",`i') 
               local time1=word("`t1'",`i')
               local newname = subinstr("`time2'","_2","",.) //remove _2 to capture variable name
               
               gen diff_`newname' = `time2' - `time1'
            }
            good luck
            Cu

            Comment


            • #7
              Thank you Cu.

              The part worked well and I verified that
              qui d *_1, varlist qui d *_2, varlist were picking the correct variables.

              In the foreach part, unfortunately, I am getting an 'invalid syntax' error?
              Any further ideas?

              Best,
              Nikos

              Comment


              • #8
                Originally posted by Nikolaos Pandis View Post
                Thank you Cu.

                The part worked well and I verified that
                qui d *_1, varlist qui d *_2, varlist were picking the correct variables.

                In the foreach part, unfortunately, I am getting an 'invalid syntax' error?
                Any further ideas?

                Best,
                Nikos
                I dont know which is your stata version, in my version 15.1 it worked well. See below is my test case:

                . clear

                . set obs 10
                number of observations (_N) was 0, now 10

                . gen a_1=1

                . gen a_2=2

                . gen b_1=1

                . gen b_2=2

                . gen c_1=1

                . gen c_2=2

                . qui d *_2, varlist

                . local t2 `r(varlist)' // capture t2 variables

                . qui d *_1, varlist
                . local t1 `r(varlist)' // capture t1 variables
                . set trace on
                . foreach i of numlist 1/`=wordcount("`t2'")' {
                2. //looping through each variable in the t1, t2 list
                . local time2 = word("`t2'",`i')
                3. local time1 = word("`t1'",`i')
                4. local newname = subinstr("`time2'","_2","",.) //remove _2 to capture variable name
                5.
                . gen diff_`newname' = `time2' - `time1'
                6. }
                - foreach i of numlist 1/`=wordcount("`t2'")' {
                = foreach i of numlist 1/3 {
                - local time2 = word("`t2'",`i')
                = local time2 = word("a_2 b_2 c_2",1)
                - local time1 = word("`t1'",`i')
                = local time1 = word("a_1 b_1 c_1",1)
                - local newname = subinstr("`time2'","_2","",.)
                = local newname = subinstr("a_2","_2","",.)
                - gen diff_`newname' = `time2' - `time1'
                = gen diff_a = a_2 - a_1
                - }
                - local time2 = word("`t2'",`i')
                = local time2 = word("a_2 b_2 c_2",2)
                - local time1 = word("`t1'",`i')
                = local time1 = word("a_1 b_1 c_1",2)
                - local newname = subinstr("`time2'","_2","",.)
                = local newname = subinstr("b_2","_2","",.)
                - gen diff_`newname' = `time2' - `time1'
                = gen diff_b = b_2 - b_1
                - }
                - local time2 = word("`t2'",`i')
                = local time2 = word("a_2 b_2 c_2",3)
                - local time1 = word("`t1'",`i')
                = local time1 = word("a_1 b_1 c_1",3)
                - local newname = subinstr("`time2'","_2","",.)
                = local newname = subinstr("c_2","_2","",.)
                - gen diff_`newname' = `time2' - `time1'
                = gen diff_c = c_2 - c_1
                - }

                Comment


                • #9
                  Thank you Cu for your help.. I will check asap. Best, Nikos

                  Comment


                  • #10
                    Dear Cu,
                    I was running the code in 2 steps as I was trying to understand what it does. This was the reason it was failing.
                    When I run as one it works great!
                    Thank you very much and have a good week.
                    Best wishes,
                    Nikos

                    Comment


                    • #11
                      Originally posted by Nikolaos Pandis View Post
                      Dear Cu,
                      I was running the code in 2 steps as I was trying to understand what it does. This was the reason it was failing.
                      When I run as one it works great!
                      Thank you very much and have a good week.
                      Best wishes,
                      Nikos
                      Hi Nikos,

                      the problem is that if you run "local" in the dofile the value capture only when you run it. So after you run step1 in the dofile local t1 and local t2 is null :> nothing to run step2
                      local command only keep value after execute it in command window

                      Best,
                      Cu

                      Comment


                      • #12
                        Yes, I realized that after. many thanks for your help
                        Best wishes,
                        Nikos

                        Comment

                        Working...
                        X