Announcement

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

  • How to apply a loop when assert is repeated across multiple variables

    Hi Statalist members,
    I have a dataset with the same variable from different sources. I want to verify if values from the two sources are the same for all variables. How can I use a loop to avoid repeating the code several times?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int date double(mai2 ric2 rop2 be2 so2 fm2 pet2 di2 wor2 mai1 ric1 rop1 be1 so1 fm1 pet1 di1 wor1)
    16102 25957 54761 22445 39870 29685 .  809.561403508772  745.469298245614 249.16165107692305 25957 54761 22445 39870 29685 .  809.561403508772  745.469298245614 249.16165107692305
    16131 25816 56008 21248 36824 27664 .           811.975 756.3421052631579  271.0426618461538 25816 56008 21248 36824 27664 .           811.975 756.3421052631579  271.0426618461538
    16162 21154 58360 20769 34420 26467 . 824.4333333333332 756.3421052631579  290.6060310769231 21154 58360 20769 34420 26467 . 824.4333333333332 756.3421052631579  290.6060310769231
    16192 15855 54855 20411 32007 28150 . 901.2833333333332 752.1296296296296  326.8213704615384 15855 54855 20411 32007 28150 . 901.2833333333332 752.1296296296296  326.8213704615384
    16223 13400 44663 21666 33848 22582 .          904.5125 785.6041666666667          299.77003 13400 44663 21666 33848 22582 .          904.5125 785.6041666666667          299.77003
    end
    format %tdMon-YY date
    bys date: assert mai2==mai1
    I would like to check the same for ric1==ric2, rop1==rop2 and so on. Please help and thank you in advance.

  • #2
    Code:
    local xmai ric rop be so fm pet di wor{
        bys date: assert `x'1 == `x'2
    }
    I am not sure if you need "bys date:" if the dates are unique, since the two parts have been merged by date, and assert looks across rows.
    Last edited by Ken Chui; 11 Aug 2021, 10:42.

    Comment


    • #3
      Ken,
      Thank for you for code.
      Now I have a follow up question, how do I know which variables are not the same. The code shows that assertion if false and there are 196 contradictions in 196 by-groups.

      Comment


      • #4
        First, sorry that my code in #2 is not quite right. I was thinking about setting up a local and foreach the same time. Please ignore that and use this one.

        Put a display command so that it'd tell you which one it's working on, and it should show where it's stalled.

        Code:
        foreach x in mai ric rop be so fm pet di wor{
            display "`x'"
            bys date: assert `x'1 == `x'2
        }
        Last edited by Ken Chui; 11 Aug 2021, 11:40.

        Comment


        • #5
          Thank you. The code worked.

          Comment

          Working...
          X