Announcement

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

  • global replace

    Hello I have a varlist of variables (ex.10 variables) for 2017 and a varlist that contain same variables but for 2018 (10 variables). I would like to join them in 10 columns containing 2017 and then 2018 all together in the same column. But I would like to do it directly for all the variables without doing it one variable at the time. Is ther a way to do it?

    Ex. of variables from first varlist 2017
    T1_1_FirstClick T1_1_LastClick T1_1_PageSubmit T1_2_FirstClick T1_2_LastClick T1_2_PageSubmit --_> global time2017



    the same variables for second varlist 2018
    T1_1_First_Click T1_1_Last_Click T1_1_Page_Submit T1_2_First_Click T1_2_Last_Click T1_2_Page_Submit --> global time2018

    each variable has a number of values and then empty cells where it corresponds to the values for 2018 and viceversa for 2018

    T1_1_FirstClick

    349
    4859
    5930
    39595
    .
    .
    .
    .

    while

    T1_1_First_Click
    .
    .
    .
    .
    5758
    58483
    48584
    5858

    I tried a global but cannot understand how to work together with two globals.

    foreach w of global time2017 {
    g `w'_new= `w'

    foreach w of global time2018 {
    replace XXXX `w' if XXX=.

    here XXXX should be the corresponding variable of the newly created varibales T1*_new. But stata does not accept a varlist there
    Last edited by giulia leila; 13 Sep 2018, 08:47.

  • #2
    I'm confused since you can't have two variables with exactly the same name in Stata; are these in two different files? if so, use -append-; otherwise please read the FAQ and, following the advice in the FAQ, clarify what you have and use -dataex- to show some example data; see
    Code:
    help append

    Comment


    • #3
      hello, thank you very much for replying. Actually the names are slightly different (there is an extra _ in the second group of variables) and they are on the same file. they are from two surveys (2017 and 2018) so by mistake they were put in the file with slightly different names and they did not match (even if they are the same variable)
      Last edited by giulia leila; 13 Sep 2018, 09:57.

      Comment


      • #4
        here is an example from dataex


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input double(T1_1_FirstClick T1_1_LastClick T1_2_FirstClick T1_1_First_Click T1_1_Last_Click T1_2_First_Click)
        24637 24637 29688     .     .     .
        28701 28701 23207     .     .     .
        22093 22093  6.07     .     .     .
        23138 23138 12766     .     .     .
        24078 24078 17612     .     .     .
        29448 32168 20154     .     .     .
        24715 24715 36974     .     .     .
        23493 23493 28151     .     .     .
        39801 39801 35092     .     .     .
        46857 46857  1423     .     .     .
        20327 20327 28243     .     .     .
        26032 26032 44026     .     .     .
        22478 22478 21187     .     .     .
        24508 24508 31962     .     .     .
        40407 40407 34924     .     .     .
        20556 20556  8365     .     .     .
        30553 30553 26065     .     .     .
        22641 31264 42354     .     .     .
        24788 24788 30.34     .     .     .
        29726 29726 15183     .     .     .
        23409 23409 26657     .     .     .
        26282 26282 46796     .     .     .
        20003 20003 18909     .     .     .
        35382 35382 44772     .     .     .
        20838 23166 21189     .     .     .
        34271 34271 36193     .     .     .
        46564 46564 12999     .     .     .
        29739 29739 32029     .     .     .
        21912 21912  1655     .     .     .
        22402 22402 32588     .     .     .
        26695 26695  3498     .     .     .
        22135 22135 25329     .     .     .
        28354 28354 46243     .     .     .
        22338 22338 24281     .     .     .
        23315 23315 30286     .     .     .
        24869 24869 41.86     .     .     .
        22445 22445 16362     .     .     .
        21748 21748 15421     .     .     .
        23476 23476  3678     .     .     .
        23897 23897 16.06     .     .     .
        29283 29283 41171     .     .     .
        55545 55545 10173     .     .     .
        39828 39828 62921     .     .     .
        31593 31593 30437     .     .     .
        30318 30318 23195     .     .     .
        25383 25383 31132     .     .     .
        24524 24524 17147     .     .     .
        20778 20778 30332     .     .     .
        28027 28027 34.03     .     .     .
        22175 22175  1807     .     .     .
        35056 35056 11441     .     .     .
        36019 36019 34671     .     .     .
        37612 37612 47573     .     .     .
        28616 28616 28314     .     .     .
        25445 25445 17609     .     .     .
        28149 28149 18571     .     .     .
        20357 20357  3348     .     .     .
        20942 20942 31435     .     .     .
        31138 31138 31817     .     .     .
        41777 41777 60317     .     .     .
        24933 24933 18387     .     .     .
        24307 24.41 43413     .     .     .
        20006 20006 20382     .     .     .
        20332 26.62 18244     .     .     .
        36956 36956 34716     .     .     .
        22207 22207 20002     .     .     .
        22731 22731  1994     .     .     .
        25633 25633 49861     .     .     .
        44112 44112 48989     .     .     .
        28742 28742 33157     .     .     .
        32309 32309 20503     .     .     .
        20367 20367 25173     .     .     .
        24731 24731 26808     .     .     .
        20067 20067 23646     .     .     .
        24034 24034 29936     .     .     .
        20126 20126 26762     .     .     .
        20374 20374 19863     .     .     .
        23232 23232 20054     .     .     .
        22958 22958 18018     .     .     .
        28592 28592 42585     .     .     .
        23426 23426  3127     .     .     .
            .     .     . 27.06 27.06 31111
            .     .     .     .     .     .
            .     .     .     .     .     .
            .     .     .     .     .     .
            .     .     .     .     .     .
            .     .     . 17701 17701  2608
            .     .     .     .     .     .
            .     .     .     .     .     .
            .     .     .  2749  2749 33674
            .     .     .     .     .     .
            .     .     . 13322 13322  4801
            .     .     .  .979  .979 20096
            .     .     .     .     .     .
            .     .     . 11214 11214 11.11
            .     .     .     .     .     .
            .     .     .     .     .     .
            .     .     .     .     .     .
            .     .     .     .     .     .
            .     .     . 16082 16082 37515
        end

        Comment


        • #5
          from that i want to create a unique T1_1_FirstClick variable made out of (T1_1_FirstClick and T1_1_First_Click). But there are many of them so i would like to avoid going through each couple of variables individually.

          Comment


          • #6
            What you want is to loop over parallel lists (see: https://www.stata.com/support/faqs/p...arallel-lists/). Following the example on the linked page:

            Code:
            local v2017 T1_1_FirstClick T1_1_LastClick T1_2_FirstClick 
            local v2018 T1_1_First_Click T1_1_Last_Click T1_2_First_Click
            local n : word count `v2017'
            
            forvalues i=1/`n' {
                    local a : word `i' of `v2017'
                    local b : word `i' of `v2018'
                    gen `a'_new=`a'
                    replace `a'_new=`b' if `a'==.
                    }
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              An alternative to the excellent code of Carole J. Wilson

              Code:
              local v2017 T1_1_FirstClick T1_1_LastClick T1_2_FirstClick 
              local v2018 T1_1_First_Click T1_1_Last_Click T1_2_First_Click
              
              foreach v of local v2017 {
                      gettoken V v2018 : v2018 
                      gen `v'_new = cond(missing(`v'), `V', `v') 
              }
              So, you loop over the elements of one macro and simultaneously take the first element of the other macro without replacing it. (Otherwise put, it's a stack.) This lacks the symmetry of Carole's code, but it has its own style. Note that either of these expressions below would do as well as argument to generate.

              Code:
              min(`v',  `V') 
              
              max(`v', `V')

              Comment

              Working...
              X