Announcement

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

  • Export file if conditions are observed

    Hello,

    I am using Stata/BE 17 for Mac and I want to export an Excel file depending on a merge result.

    The code below works when merge==1 is observed, but when it is not, of course all observations are deleted and no n=_n is generated, and then command keep if n==1 doesn't work (n ambiguous abbreviation).

    Code:
    keep if _merge==1
    bysort id: gen n=_n
    keep if n==1
    keep id lname fname
    if c(N) > 0 {
    export excel $orig/questions.xlsx, replace firstrow(var)
    }
    Could someone help me, please? I want the code to work even if merge==1 is not observed.

    Thank you,

    Nicolas
    Last edited by Nicolas Charette; 09 Sep 2022, 13:11.

  • #2
    Well, what do you want the code to actually do if there are no observations with _merge == 1?

    Comment


    • #3
      In addition to answering the question in #2, please also provide an example of your data after merging (using dataex), when _merge == 1 is not observed.

      Comment


      • #4
        Clyde Schechter I use this part of the code between a "preserve" and "restore" segment in a much larger do-file. Additional data will come in regularly later in the process, and I use this part of the code to flag certain problems in the data gathering process when there are observations for _merge==1. If there are no observations with _merge==1, it means there are no problems and I just want the code to run smoothly and not stop.
        Last edited by Nicolas Charette; 09 Sep 2022, 19:47.

        Comment


        • #5
          Hemanshu Kumar

          Here is a shorter version of the data. I have erased lname and fname for anonymity.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input long id int annee byte(note _merge)
          8198541 2011 20 3
          8198541 2016 48 3
          8198541 2011 33 3
          8198541 2019 56 3
          8198541 2019 79 3
          8198541 2011 20 3
          8198541 2016 23 3
          8198541 2012 40 3
          8198541 2015 83 3
          8206906 2017 68 3
          8206906 2012 78 3
          8206906 2016 59 3
          8206906 2016 56 3
          8206906 2015 50 3
          8206906 2017 66 3
          8206906 2015 78 3
          8206906 2014 52 3
          8206906 2011 52 3
          8206906 2016 55 3
          8206906 2016 80 3
          8206906 2016 67 3
          8206906 2015 80 3
          8206906 2012 77 3
          8206906 2012 64 3
          8206906 2009 84 3
          8206906 2009 77 3
          8206906 2013 73 3
          8206906 2017 67 3
          8206906 2009 73 3
          8206906 2017 72 3
          8206906 2009 80 3
          8206906 2013 50 3
          8206906 2014 82 3
          8206906 2015 79 3
          8206906 2011 85 3
          8206906 2012 51 3
          8206906 2016 53 3
          8206906 2016 72 3
          8206906 2012 60 3
          8206906 2011 62 3
          8206906 2016 54 3
          8206906 2009 61 3
          8206906 2017 60 3
          8206906 2015 76 3
          8206906 2016 71 3
          8206906 2013 60 3
          8206906 2009 55 3
          8206906 2011 65 3
          end
          label values _merge _merge
          label def _merge 3 "Matched (3)", modify

          Comment


          • #6
            With that added info from dataex and my comment (#4), what I wrote in my initial post would look more like:

            Code:
            preserve
            keep if _merge==1
            bysort id: gen n=_n
            keep if n==1
            keep id  
            if c(N) > 0 {
                   export excel $orig/questions.xlsx, replace firstrow(var)
            }
            restore
            My intuition is that I would have to code something that starts with "if merge==1 is observed" kind of command...
            Last edited by Nicolas Charette; 09 Sep 2022, 19:45.

            Comment


            • #7
              This should do it:

              Code:
              preserve
                  count if _merge == 1
                  if `r(N)' {
                      keep if _merge==1
                      bysort id: gen n=_n
                      keep if n==1
                      keep id  
                      if c(N) > 0 {
                             export excel $orig/questions.xlsx, replace firstrow(var)
                      }
                  }
              restore

              Comment


              • #8
                Thank you ! It worked fine.

                N.

                Comment

                Working...
                X