Announcement

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

  • something wrong with do swapval

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(founderEduBgnTm1Y founderEduBgnTm2Y founderEduBgnTm3Y)
    2013 2006 2002
    end
    
    swapval  founderEduBgnTm1Y  founderEduBgnTm2Y if   founderEduBgnTm2Y < founderEduBgnTm1Y
    Click image for larger version

Name:	1.png
Views:	1
Size:	40.9 KB
ID:	1782848
    Last edited by Fred Lee; 01 Nov 2025, 20:53.

  • #2
    Thanks for the data example.

    You're asked to explain where community-contributed commands come from (their provenance) (FAQ Advice #12), Also, please don't use screenshots (same place).

    swapval is an old command from SSC first written in 1999. I am the author and it was not even on my current machine until I just (re-)installed it. Still, it should still do what it was intended to do. Perhaps it's not sufficiently well explained.

    The design of swapval was poor in this sense. If what you ask for isn't what you want, then you've overwritten the original data, which could be a problem.

    I imagine, however, that it isn't what you should want here. Your variable names confuse here (as shown by your screenshot, but if Tm1, Tm2, Tm3 should be in ascending order, then use rowsort from the Stata Journal

    Code:
    . * Example generated by -dataex-. For more info, type help dataex
    . clear
    
    . input float(founderEduBgnTm1Y founderEduBgnTm2Y founderEduBgnTm3Y)
    
         founde~1Y  founde~2Y  founde~3Y
      1. 2013 2006 2002
      2. end
    
    . 
    . rename (founderEduBgn*) (*)
    
    . 
    . list 
    
         +--------------------+
         | Tm1Y   Tm2Y   Tm3Y |
         |--------------------|
      1. | 2013   2006   2002 |
         +--------------------+
    
    . 
    . save work, replace 
    (file work.dta not found)
    file work.dta saved
    
    . 
    . swapval  Tm1Y  Tm2Y if   Tm2Y < Tm1Y
    
    . 
    . list 
    
         +--------------------+
         | Tm1Y   Tm2Y   Tm3Y |
         |--------------------|
      1. | 2013   2013   2002 |
         +--------------------+
    
    . 
    . use work, clear 
    
    . 
    . rowsort Tm?Y, gen(Tm1 Tm2 Tm3) 
    
    . 
    . list 
    
         +-----------------------------------------+
         | Tm1Y   Tm2Y   Tm3Y    Tm1    Tm2    Tm3 |
         |-----------------------------------------|
      1. | 2013   2006   2002   2002   2006   2013 |
         +-----------------------------------------+
    Is that what you want?

    Comment


    • #3
      Something odd in the forum software, or my set-up, often inhibits my replying in a thread, but the same content is accepted as a new thread.

      For a reply please see https://www.statalist.org/forums/for...ly-to-fred-lee

      Comment


      • #4
        Yes, this is exact what I want. The rowsort solves my problem. I am wondering if I want exchange values between variables, do we have alternative ados. Or I will exchange values via own syntax.

        Comment


        • #5
          Yes, this is the first step what I want. I am wondering if I want exchange values between variables, do we have alternative ados. Or I will exchange values via own syntax. In my detailed example, I want to order the Univ* acoording to year, rather than order the year only.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int(founderEduBgnTm1Y founderEduBgnTm2Y founderEduBgnTm3Y) str81 founderUniv1 str96 founderUniv2 str87 founderUniv3
          2003 2010 . "清华大学" "Warwick Business School" ""
          2010 2003 . "清华大学" "Warwick Business School" ""
          end

          Comment


          • #6
            Nick, I'm having the same issues. Lost of errors trying to post (JSON errors if coding involved), and can't post a file.

            Comment


            • #7
              Sorry, but I can’t follow what different result you want.

              Comment


              • #8
                Thanks Nick. founderEduBgnTm1Y corresponds with founderUniv1, so I want to resort founderUniv* according to founderEduBgnTm*. Specifically, I want founderUniv1 to be high school name, founderUniv2 to be colledge name, and founderUniv3 to be graduate school name.

                Comment


                • #9
                  Intend aside, there's a reproducible bug in swapval that should probably be fixed:
                  Code:
                  . input min max
                  
                             min        max
                    1. 73 42
                    2. end
                  
                  . 
                  . list
                  
                       +-----------+
                       | min   max |
                       |-----------|
                    1. |  73    42 |
                       +-----------+
                  
                  . 
                  . swapval min max if max < min
                  
                  . 
                  . list
                  
                       +-----------+
                       | min   max |
                       |-----------|
                    1. |  73    73 |
                       +-----------+
                  The bug is caused by lines 87 and 88 in swapval.ado:
                  Code:
                          qui replace `b' = `a' `if' 
                          qui replace `a' = `copy' `if'
                  When `if' refers to b, then after the first line executes, the values of b will have changed. As a result, the second replace command will identify a different set of observations than intended.

                  Comment


                  • #10
                    daniel klein Good catch. Looking at again, the whole thing needs rewriting!

                    Comment


                    • #11
                      I believe the issue that Nick Cox and George Ford, and possibly other users are experiencing is a function of the Cloudflare protections verifying that you are human, EG:


                      Click image for larger version

Name:	human.png
Views:	1
Size:	51.0 KB
ID:	1782878


                      Should your post or response take a bit to compose, there is a chance the session cookie may have timed out; this could trigger another verification which may not appear properly and manifest as the JSON error. I appreciate everyone's patience while we dial in protecting the site while still making it usable for all.

                      -Pete

                      Comment


                      • #12
                        Pete Huckelba (StataCorp) That makes a lot of sense. I was very puzzled at the thought that the content of my post was causing problems, but it does seem more likely the length of time drafting each post would be an issue.

                        Thanks very much for the support you're giving to Statalist.

                        Comment


                        • #13
                          Thank you Pete Huckelba (StataCorp) for explaining this problem. I have thought there was something wrong with my network.

                          Comment


                          • #14
                            Referring back to #9 and #10: a fixed version of swapval is now up on SSC. Thanks to KitBaum as always for general support and to daniel klein for vigilance and insight.
                            Last edited by Nick Cox; 05 Nov 2025, 05:55.

                            Comment

                            Working...
                            X