Announcement

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

  • append fails to work

    Hi, I am new to Stata. Suppose I would like to append dataset2 to dataset 1
    dataset 1 reads:
    Name Id Age Nationality
    A 1 12 USA
    B 2 12 UK
    C 3 14 Greece
    dataset 2 reads:
    Name Id Age Nationality
    A 4 13 USA
    B 5 15 UK
    C 6 15 Greece
    my desired result:
    Name Id Age Nationality
    A 1 12 USA
    B 2 12 UK
    C 3 14 Greece
    A 4 13 USA
    B 5 15 UK
    C 6 15 Greece
    I tried
    Code:
    use dataset1.dta, clear
    and then
    Code:
    append using dataset2.dta
    did not work. There was no error but dataset2 was not added below dataset1 Can you please help me? Thank you so much!
    Last edited by Julius Fernandez; 22 Aug 2020, 14:38.

  • #2
    You don't give nearly enough information. Did the use statement work, and append fail? What was the error message? I am guessing that one or both of the files is not in .dta format. Study -import delimited- to learn how to put the files in .dta format.

    Comment


    • #3
      Originally posted by [email protected] View Post
      You don't give nearly enough information. Did the use statement work, and append fail? What was the error message? I am guessing that one or both of the files is not in .dta format. Study -import delimited- to learn how to put the files in .dta format.
      Hi
      Please read my post again. There was some format problem with the table, hence I had to fix. There was no message, it was just when I export using save the second dataset2 was not added below dataset1

      Comment


      • #4
        Either (a) the second dataset was not successfully appended or (b) the save command was incorrect or otherwise failed or (c) the save command worked, but you accidentally saved the dataset under a name you weren't expecting or a in a directory other than the one you are looking in and thus the dataset you were looking at wasn't the one you just saved.

        Comment


        • #5
          Originally posted by William Lisowski View Post
          Either (a) the second dataset was not successfully appended or (b) the save command was incorrect or otherwise failed or (c) the save command worked, but you accidentally saved the dataset under a name you weren't expecting or a in a directory other than the one you are looking in and thus the dataset you were looking at wasn't the one you just saved.
          Im sure it was (a), thats why I asked why append failed

          Comment


          • #6
            Consider the following example using your data.
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str1 name byte(id age) str6 nationality
            "A" 1 12 "USA"   
            "B" 2 12 "UK"    
            "C" 3 14 "Greece"
            end
            save "~/Downloads/ds1"
            
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str1 name byte(id age) str6 nationality
            "A" 4 13 "USA"   
            "B" 5 15 "UK"    
            "C" 6 15 "Greece"
            end
            save "~/Downloads/ds2"
            
            clear
            use "~/Downloads/ds1"
            append using "~/Downloads/ds2"
            describe
            
            save "~/Downloads/ds12"
            describe using "~/Downloads/ds12"
            The output below shows that the appended data and the saved dataset ds12 contain all 6 observations. Carefully compare this example to what you did to find where you went wrong.
            Code:
            . clear
            
            . use "~/Downloads/ds1"
            
            . append using "~/Downloads/ds2"
            
            . describe
            
            Contains data from ~/Downloads/ds1.dta
              obs:             6                          
             vars:             4                          22 Aug 2020 16:57
            ------------------------------------------------------------------------------------------------
                          storage   display    value
            variable name   type    format     label      variable label
            ------------------------------------------------------------------------------------------------
            name            str1    %9s                   
            id              byte    %8.0g                 
            age             byte    %8.0g                 
            nationality     str6    %9s                   
            ------------------------------------------------------------------------------------------------
            Sorted by: 
                 Note: Dataset has changed since last saved.
            
            . 
            . save "~/Downloads/ds12"
            file ~/Downloads/ds12.dta saved
            
            . describe using "~/Downloads/ds12"
            
            Contains data                                 
              obs:             6                          22 Aug 2020 16:57
             vars:             4                          
            ------------------------------------------------------------------------------------------------
                          storage   display    value
            variable name   type    format     label      variable label
            ------------------------------------------------------------------------------------------------
            name            str1    %9s                   
            id              byte    %8.0g                 
            age             byte    %8.0g                 
            nationality     str6    %9s                   
            ------------------------------------------------------------------------------------------------
            Sorted by: 
            
            .

            Comment


            • #7
              Originally posted by William Lisowski View Post
              Consider the following example using your data.
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str1 name byte(id age) str6 nationality
              "A" 1 12 "USA"
              "B" 2 12 "UK"
              "C" 3 14 "Greece"
              end
              save "~/Downloads/ds1"
              
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str1 name byte(id age) str6 nationality
              "A" 4 13 "USA"
              "B" 5 15 "UK"
              "C" 6 15 "Greece"
              end
              save "~/Downloads/ds2"
              
              clear
              use "~/Downloads/ds1"
              append using "~/Downloads/ds2"
              describe
              
              save "~/Downloads/ds12"
              describe using "~/Downloads/ds12"
              The output below shows that the appended data and the saved dataset ds12 contain all 6 observations. Carefully compare this example to what you did to find where you went wrong.
              Code:
              . clear
              
              . use "~/Downloads/ds1"
              
              . append using "~/Downloads/ds2"
              
              . describe
              
              Contains data from ~/Downloads/ds1.dta
              obs: 6
              vars: 4 22 Aug 2020 16:57
              ------------------------------------------------------------------------------------------------
              storage display value
              variable name type format label variable label
              ------------------------------------------------------------------------------------------------
              name str1 %9s
              id byte %8.0g
              age byte %8.0g
              nationality str6 %9s
              ------------------------------------------------------------------------------------------------
              Sorted by:
              Note: Dataset has changed since last saved.
              
              .
              . save "~/Downloads/ds12"
              file ~/Downloads/ds12.dta saved
              
              . describe using "~/Downloads/ds12"
              
              Contains data
              obs: 6 22 Aug 2020 16:57
              vars: 4
              ------------------------------------------------------------------------------------------------
              storage display value
              variable name type format label variable label
              ------------------------------------------------------------------------------------------------
              name str1 %9s
              id byte %8.0g
              age byte %8.0g
              nationality str6 %9s
              ------------------------------------------------------------------------------------------------
              Sorted by:
              
              .
              Hi
              Thank you so much for your help! I realized the problem, there were blank rows right below both dataset. I have to drop the blank rows

              Comment


              • #8
                It's important to me that we not leave those who find this topic at some later time with the impression that append is unreliable.

                So to be clear, append did not fail. Your input wasn't as you thought it was, so the output didn't look like you expected it to look. The problem was that you somehow had extra observations in both datasets that you didn't notice.

                Comment


                • #9
                  Originally posted by William Lisowski View Post
                  It's important to me that we not leave those who find this topic at some later time with the impression that append is unreliable.

                  So to be clear, append did not fail. Your input wasn't as you thought it was, so the output didn't look like you expected it to look. The problem was that you somehow had extra observations in both datasets that you didn't notice.
                  Hi,
                  How should I do that? Should I delete the thread? I cannot do that though

                  Comment


                  • #10
                    No action is required by you, my discussion at post #8 served the purpose. And this topic is helpful and worth retaining, even if not for the reason you expected, because it does point out an interesting reason why you may not see the results you expect when appending two datasets.

                    In general, I'd say that my experience on Stata list with questions of the nature of "This Stata command didn't work" are much more often than not what we used to classify, in a Technical Services organization I worked for, as "PEBCAK" which is an acronym for "Problem Exists Between Chair And Keyboard". So examples of ways commands can work but not give the results you expect may benefit someone between the chair and keyboard at a later date.

                    Had you chosen as a title for this topic something more like "Problem using append" with the discussion in posts #1, #3, and #5 less positive about the cause of your unexpected results, there would have been been nothing to correct for the benefit of future readers.

                    Comment

                    Working...
                    X