Announcement

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

  • Generate new variable(s) conditional on different variables taking a specific value

    Dears,

    I would generate new variables every time that corr_tof* takes the same value as tof. Here below the dataex

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float id int year long(type_cert1 cert1 product_cert1) float corr_tof1 long(type_cert2 cert2 product_cert2) float(corr_tof2 tof)
    1 2008 6 1 22 8 0 0 0 0 10
    1 2009 6 1 22 8 0 0 0 0 10
    2 2008 6 1 22 8 0 0 0 0 10
    3 2008 6 1 22 8 0 0 0 0 10
    4 2008 6 1 22 8 0 0 0 0 10
    4 2009 6 1 22 8 0 0 0 0 8
    4 2011 6 1 22 8 0 0 0 0 8
    4 2012 6 1 22 8 0 0 0 0 8
    5 2008 6 1 22 8 0 0 0 0 8
    6 2008 6 1 22 8 0 0 0 0 10
    7 2008 6 1 22 8 0 0 0 0 10
    8 2008 6 1 22 8 0 0 0 0 10
    8 2014 6 1 22 8 0 0 0 0 8
    9 2014 1 1 64 4 0 0 0 0 14
    9 2016 1 1 64 4 0 0 0 0 7
    9 2017 1 1 64 4 0 0 0 0 7
    9 2018 1 1 64 4 0 0 0 0 14
    9 2019 1 1 64 4 0 0 0 0 14
    10 2008 6 1 22 8 0 0 0 0 10
    11 2008 6 1 22 8 0 0 0 0 10
    12 2008 6 1 22 8 0 0 0 0 10
    12 2009 1 1 9 10 0 0 0 0 10
    12 2010 1 1 9 10 0 0 0 0 8
    12 2011 1 1 9 10 0 0 0 0 10
    12 2012 1 1 9 10 0 0 0 0 8
    12 2014 6 1 22 8 0 0 0 0 8
    12 2015 6 1 22 8 0 0 0 0 8
    13 2008 6 1 22 8 0 0 0 0 10
    13 2009 1 1 9 10 0 0 0 0 10
    14 2008 6 1 22 8 0 0 0 0 10
    14 2009 6 1 22 8 0 0 0 0 10
    14 2011 6 1 22 8 0 0 0 0 8
    15 2008 6 1 22 8 0 0 0 0 10
    15 2009 6 1 22 8 0 0 0 0 10
    16 2018 3 6 64 4 0 0 0 0 8
    16 2019 3 6 64 4 0 0 0 0 8
    17 2009 6 1 22 8 0 0 0 0 10
    17 2011 6 1 22 8 0 0 0 0 8
    18 2008 3 6 64 4 0 0 0 0 10
    18 2010 3 6 64 4 0 0 0 0 10
    18 2011 3 6 64 4 0 0 0 0 8
    19 2012 6 1 22 8 0 0 0 0 8
    20 2008 6 1 22 8 0 0 0 0 10
    21 2008 6 1 22 8 0 0 0 0 10
    22 2008 6 1 22 8 0 0 0 0 10
    22 2011 6 1 22 8 0 0 0 0 8
    23 2008 6 1 22 8 0 0 0 0 10
    23 2011 6 1 22 8 0 0 0 0 8
    23 2012 6 1 22 8 0 0 0 0 8
    23 2014 1 1 9 10 6 1 22 8 10
    23 2016 6 1 22 8 0 0 0 0 8
    23 2017 6 1 22 8 0 0 0 0 8
    24 2008 6 1 22 8 0 0 0 0 10
    25 2008 6 1 22 8 0 0 0 0 8
    26 2009 1 1 9 10 0 0 0 0 10
    26 2010 1 1 9 10 0 0 0 0 8
    26 2011 1 1 9 10 0 0 0 0 8
    26 2012 1 1 9 10 0 0 0 0 8
    26 2013 6 1 22 8 0 0 0 0 8
    27 2008 6 1 99 4 3 6 64 4 4
    27 2010 3 6 64 4 0 0 0 0 4
    27 2011 3 6 64 4 0 0 0 0 4
    27 2012 3 6 64 4 0 0 0 0 4
    27 2013 3 6 64 4 0 0 0 0 4
    27 2014 3 6 64 4 0 0 0 0 4
    27 2015 8 3 103 4 3 6 64 4 4
    27 2016 3 6 64 4 6 1 103 4 4
    27 2017 3 6 64 4 0 0 0 0 4
    27 2018 3 6 64 4 0 0 0 0 4
    27 2019 3 6 64 4 0 0 0 0 4
    28 2008 6 1 99 4 0 0 0 0 7
    28 2010 1 1 64 4 0 0 0 0 7
    28 2011 6 1 103 4 3 6 64 4 4
    28 2012 8 3 99 4 3 6 64 4 4
    28 2013 3 6 64 4 1 1 64 4 4
    28 2014 1 1 64 4 3 6 64 4 7
    28 2016 8 3 103 4 1 1 64 4 7
    28 2017 1 1 64 4 3 6 64 4 7
    28 2018 3 6 64 4 1 1 64 4 7
    28 2019 1 1 64 4 3 6 64 4 7
    29 2009 1 1 9 10 0 0 0 0 10
    29 2010 3 6 64 4 1 1 9 10 8
    29 2011 3 6 64 4 0 0 0 0 8
    29 2012 3 6 64 4 0 0 0 0 8
    29 2013 1 1 9 10 3 6 64 4 8
    29 2014 3 6 64 4 0 0 0 0 8
    29 2015 3 6 64 4 0 0 0 0 8
    30 2008 6 1 22 8 0 0 0 0 10
    31 2008 6 1 22 8 0 0 0 0 10
    32 2008 6 1 22 8 0 0 0 0 10
    32 2009 1 1 9 10 0 0 0 0 10
    33 2009 1 1 9 10 0 0 0 0 10
    34 2008 6 1 38 8 0 0 0 0 10
    35 2008 6 1 22 8 0 0 0 0 10
    35 2010 1 1 9 10 0 0 0 0 8
    35 2011 1 1 9 10 0 0 0 0 8
    35 2012 1 1 9 10 0 0 0 0 8
    36 2009 6 1 22 8 0 0 0 0 10
    36 2010 6 1 22 8 0 0 0 0 8
    37 2008 6 1 22 8 0 0 0 0 10
    end
    What I would like to do is

    Code:
    // Generate a new variable containing the same as the related original variable ONLT IF corr_tof*=tof 
    
    g new_type_cert1=type_cert1 if corr_tof1==tof
    g new_cert1=cert1 if corr_tof1==tof
    g new_product_cert1=product_cert1 if corr_tof1==tof
    Since I have type_cert* cert* product_cert* going up to 20 in my dataset, I would like to understand how to do this in a less tedious and smarter way.

    I (naively) tried the following:

    Code:
    global corr_tof corr_tof1 corr_tof2
    
    foreach X of varlist type_cert1 type_cert2 cert1 cert2 product_cert1 product_cert2 {
    g new_`X'=`X' if $corr_tof==tof
    }
    but, besides not being sure this would produce the output I had in mind, the error message is the following:

    invalid 'corr_tof2'
    r(198)

    Many thanks,

    F.

  • #2
    Federico:
    you may want to consider:
    Code:
    . foreach var of varlist  type_cert* cert* product_cert* {
      2. g new_`var'=.
      3. }
    
    . foreach var of varlist type_cert* cert* product_cert*{
      2. foreach x of varlist corr_tof* {
      3. replace new_`var'=`var' if `x'==tof
      4. }
      5. }
    
    
    .
    Last edited by Carlo Lazzaro; 19 Oct 2021, 04:15.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Dear Carlo,

      many thanks for your prompt reply, a clear smart and efficient code!!

      It works perfectly fine, the only issue is that when

      Code:
      replace new_`var'=`var' if `x'==tof
      it replaces for ALL new_`var' while I would like to have it replaced only for those type_cert* cert* product_cert* for which
      Code:
      corr_tof*==tof
      .

      Here below the (new)dataex after implementing your code where the issue is perhaps more explicit (see the comment attached to the first observation):

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int year float(id tof corr_tof1 corr_tof2) long type_cert1 float new_type_cert1 long product_cert1 float new_product_cert1 long type_cert2 float new_type_cert2 long product_cert2 float new_product_cert2
      2008 13582  4 6 4 1 1 50 50 4 4 64 64 // Here for instance we have that tof==corr_tof2 only, but values have been replaced also for those variables related to corr_tof1
      2008 13583  1 6 0 1 . 50  . 0 .  0  .
      2008 13584 12 6 4 1 . 50  . 3 . 63  .
      2008 13585  7 4 6 4 . 64  . 1 . 50  .
      2008 13586  7 6 4 1 . 50  . 3 . 64  .
      2008 13587  4 6 4 1 1 50 50 3 3 64 64
      2008 13588  7 6 4 1 . 50  . 3 . 63  .
      2008 13589  7 6 4 1 . 50  . 3 . 64  .
      2008 13590  4 6 4 1 1 50 50 3 3 64 64
      2008 13592  1 0 0 0 .  0  . 0 .  0  .
      2008 13593 10 0 0 0 .  0  . 0 .  0  .
      2008 13594 14 0 0 0 .  0  . 0 .  0  .
      2008 13595  6 0 0 0 .  0  . 0 .  0  .
      2008 13596 14 0 0 0 .  0  . 0 .  0  .
      2008 13597  4 0 0 0 .  0  . 0 .  0  .
      2008 13598  9 0 0 0 .  0  . 0 .  0  .
      2008 13599  2 0 0 0 .  0  . 0 .  0  .
      2008 13600  1 0 0 0 .  0  . 0 .  0  .
      2008 13602  2 0 0 0 .  0  . 0 .  0  .
      2008 13603 12 0 0 0 .  0  . 0 .  0  .
      2008 13604 14 0 0 0 .  0  . 0 .  0  .
      2008 13605 10 0 0 0 .  0  . 0 .  0  .
      2008 13606 14 0 0 0 .  0  . 0 .  0  .
      2008 13608  7 4 6 5 . 63  . 1 . 50  .
      2008 13609  9 0 0 0 .  0  . 0 .  0  .
      2008 13610 14 0 0 0 .  0  . 0 .  0  .
      2008 13611  9 0 0 0 .  0  . 0 .  0  .
      2008 13612 10 0 0 0 .  0  . 0 .  0  .
      2008 13613 12 0 0 0 .  0  . 0 .  0  .
      2008 13614  1 0 0 0 .  0  . 0 .  0  .
      2008 13616  1 0 0 0 .  0  . 0 .  0  .
      2008 13617  3 0 0 0 .  0  . 0 .  0  .
      2008 13618 12 0 0 0 .  0  . 0 .  0  .
      2008 13623 11 0 0 0 .  0  . 0 .  0  .
      2008 13624  4 0 0 0 .  0  . 0 .  0  .
      2008 13625  8 0 0 0 .  0  . 0 .  0  .
      2008 13626 14 0 0 0 .  0  . 0 .  0  .
      2008 13627  3 6 0 1 . 50  . 0 .  0  .
      2008 13628  9 0 0 0 .  0  . 0 .  0  .
      2008 13629  2 0 0 0 .  0  . 0 .  0  .
      2008 13630  7 0 0 0 .  0  . 0 .  0  .
      2008 13631  1 0 0 0 .  0  . 0 .  0  .
      2008 13632  1 0 0 0 .  0  . 0 .  0  .
      2008 13633  7 0 0 0 .  0  . 0 .  0  .
      2008 13636  3 0 0 0 .  0  . 0 .  0  .
      2008 13637  2 0 0 0 .  0  . 0 .  0  .
      2008 13638  5 0 0 0 .  0  . 0 .  0  .
      2008 13639  1 0 0 0 .  0  . 0 .  0  .
      2008 13640 11 0 0 0 .  0  . 0 .  0  .
      2008 13641 14 0 0 0 .  0  . 0 .  0  .
      2008 13642  2 4 0 4 . 64  . 0 .  0  .
      2008 13643  2 0 0 0 .  0  . 0 .  0  .
      2008 13645 14 0 0 0 .  0  . 0 .  0  .
      2008 13646  3 0 0 0 .  0  . 0 .  0  .
      2008 13647  4 0 0 0 .  0  . 0 .  0  .
      2008 13648  7 0 0 0 .  0  . 0 .  0  .
      2008 13650  9 0 0 0 .  0  . 0 .  0  .
      2008 13651  1 0 0 0 .  0  . 0 .  0  .
      2008 13652  3 0 0 0 .  0  . 0 .  0  .
      2008 13653  9 0 0 0 .  0  . 0 .  0  .
      2008 13654  3 0 0 0 .  0  . 0 .  0  .
      2008 13655  2 0 0 0 .  0  . 0 .  0  .
      2008 13656 12 6 4 6 . 85  . 5 . 64  .
      2008 13657  2 0 0 0 .  0  . 0 .  0  .
      2008 13658  1 0 0 0 .  0  . 0 .  0  .
      2008 13659  1 0 0 0 .  0  . 0 .  0  .
      2008 13660  3 0 0 0 .  0  . 0 .  0  .
      2008 13661  3 0 0 0 .  0  . 0 .  0  .
      2008 13662  3 0 0 0 .  0  . 0 .  0  .
      2008 13663  1 0 0 0 .  0  . 0 .  0  .
      2008 13664 12 6 0 1 . 50  . 0 .  0  .
      2008 13665  4 6 4 1 1 50 50 3 3 64 64
      2008 13666  1 0 0 0 .  0  . 0 .  0  .
      2008 13668  9 0 0 0 .  0  . 0 .  0  .
      2008 13669  6 0 0 0 .  0  . 0 .  0  .
      end
      Do you have further suggestions for tackling this?

      Thanks again

      Comment


      • #4
        Federico:
        you may want to try:
        Code:
        . foreach var of varlist type_cert* cert* product_cert*{
          2. foreach x of varlist corr_tof* {
          3. g b=`x'
          4. replace new_`var'=`var' if b==tof
          5. drop b
          6. }
          7. }
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Dear Carlo,

          Thanks again! unfortunately it does not solve the issue (see the dataex below after running your code): even if only the corr_tof2==tof is true, values are replaced also for all variables related to corr_tof1: new_type_cert1; new_product_cert1

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int year float(id tof corr_tof1 corr_tof2) long type_cert1 float new_type_cert1 long product_cert1 float new_product_cert1 long type_cert2 float new_type_cert2 long product_cert2 float new_product_cert2
          
          2008 13582  4 6 4 1 1 50 50 4 4 64 64 //I took the same id as per the previous code ran, the output does not change
          2008 13583  1 6 0 1 . 50  . 0 .  0  .
          2008 13584 12 6 4 1 . 50  . 3 . 63  .
          2008 13585  7 4 6 4 . 64  . 1 . 50  .
          2008 13586  7 6 4 1 . 50  . 3 . 64  .
          2008 13587  4 6 4 1 1 50 50 3 3 64 64
          2008 13588  7 6 4 1 . 50  . 3 . 63  .
          2008 13589  7 6 4 1 . 50  . 3 . 64  .
          2008 13590  4 6 4 1 1 50 50 3 3 64 64
          2008 13592  1 0 0 0 .  0  . 0 .  0  .
          2008 13593 10 0 0 0 .  0  . 0 .  0  .
          2008 13594 14 0 0 0 .  0  . 0 .  0  .
          2008 13595  6 0 0 0 .  0  . 0 .  0  .
          2008 13596 14 0 0 0 .  0  . 0 .  0  .
          2008 13597  4 0 0 0 .  0  . 0 .  0  .
          2008 13598  9 0 0 0 .  0  . 0 .  0  .
          2008 13599  2 0 0 0 .  0  . 0 .  0  .
          2008 13600  1 0 0 0 .  0  . 0 .  0  .
          2008 13602  2 0 0 0 .  0  . 0 .  0  .
          2008 13603 12 0 0 0 .  0  . 0 .  0  .
          2008 13604 14 0 0 0 .  0  . 0 .  0  .
          2008 13605 10 0 0 0 .  0  . 0 .  0  .
          2008 13606 14 0 0 0 .  0  . 0 .  0  .
          2008 13608  7 4 6 5 . 63  . 1 . 50  .
          2008 13609  9 0 0 0 .  0  . 0 .  0  .
          2008 13610 14 0 0 0 .  0  . 0 .  0  .
          2008 13611  9 0 0 0 .  0  . 0 .  0  .
          2008 13612 10 0 0 0 .  0  . 0 .  0  .
          2008 13613 12 0 0 0 .  0  . 0 .  0  .
          2008 13614  1 0 0 0 .  0  . 0 .  0  .
          2008 13616  1 0 0 0 .  0  . 0 .  0  .
          2008 13617  3 0 0 0 .  0  . 0 .  0  .
          2008 13618 12 0 0 0 .  0  . 0 .  0  .
          2008 13623 11 0 0 0 .  0  . 0 .  0  .
          2008 13624  4 0 0 0 .  0  . 0 .  0  .
          2008 13625  8 0 0 0 .  0  . 0 .  0  .
          2008 13626 14 0 0 0 .  0  . 0 .  0  .
          2008 13627  3 6 0 1 . 50  . 0 .  0  .
          2008 13628  9 0 0 0 .  0  . 0 .  0  .
          2008 13629  2 0 0 0 .  0  . 0 .  0  .
          2008 13630  7 0 0 0 .  0  . 0 .  0  .
          2008 13631  1 0 0 0 .  0  . 0 .  0  .
          2008 13632  1 0 0 0 .  0  . 0 .  0  .
          2008 13633  7 0 0 0 .  0  . 0 .  0  .
          2008 13636  3 0 0 0 .  0  . 0 .  0  .
          2008 13637  2 0 0 0 .  0  . 0 .  0  .
          2008 13638  5 0 0 0 .  0  . 0 .  0  .
          2008 13639  1 0 0 0 .  0  . 0 .  0  .
          2008 13640 11 0 0 0 .  0  . 0 .  0  .
          2008 13641 14 0 0 0 .  0  . 0 .  0  .
          2008 13642  2 4 0 4 . 64  . 0 .  0  .
          2008 13643  2 0 0 0 .  0  . 0 .  0  .
          2008 13645 14 0 0 0 .  0  . 0 .  0  .
          2008 13646  3 0 0 0 .  0  . 0 .  0  .
          2008 13647  4 0 0 0 .  0  . 0 .  0  .
          2008 13648  7 0 0 0 .  0  . 0 .  0  .
          2008 13650  9 0 0 0 .  0  . 0 .  0  .
          2008 13651  1 0 0 0 .  0  . 0 .  0  .
          2008 13652  3 0 0 0 .  0  . 0 .  0  .
          2008 13653  9 0 0 0 .  0  . 0 .  0  .
          2008 13654  3 0 0 0 .  0  . 0 .  0  .
          2008 13655  2 0 0 0 .  0  . 0 .  0  .
          2008 13656 12 6 4 6 . 85  . 5 . 64  .
          2008 13657  2 0 0 0 .  0  . 0 .  0  .
          2008 13658  1 0 0 0 .  0  . 0 .  0  .
          2008 13659  1 0 0 0 .  0  . 0 .  0  .
          2008 13660  3 0 0 0 .  0  . 0 .  0  .
          2008 13661  3 0 0 0 .  0  . 0 .  0  .
          2008 13662  3 0 0 0 .  0  . 0 .  0  .
          2008 13663  1 0 0 0 .  0  . 0 .  0  .
          2008 13664 12 6 0 1 . 50  . 0 .  0  .
          2008 13665  4 6 4 1 1 50 50 3 3 64 64
          2008 13666  1 0 0 0 .  0  . 0 .  0  .
          2008 13668  9 0 0 0 .  0  . 0 .  0  .
          2008 13669  6 0 0 0 .  0  . 0 .  0  .
          end

          I guess this is because

          Code:
           
           replace new_`var'=`var' if b==tof
          correctly replace the value for all new_`var' whenever the condition b==tof is satisfied for ANY corr_tof*==tof (e.g., although only corr_tof 13==tof, the whole range of new_`var' will be replaced)?

          Many thanks,
          F.

          Comment


          • #6
            Federico:
            what follows should do the trick:
            Code:
            . foreach var of varlist type_cert* cert* product_cert* {
              2. gen new_`var'=.
              3. }
            
            . forval i = 1(1)2 {
              2. replace new_type_cert`i' = type_cert`i' if corr_tof`i'==tof
              3. replace new_product_cert`i' = product_cert`i' if corr_tof`i'==tof
              4. replace new_cert`i' = cert`i' if corr_tof`i'==tof
              5. }
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Dear Carlo,

              it works perfectly fine, thank you very much!

              Best regards
              Federico

              Comment

              Working...
              X