Announcement

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

  • Why isn't my database not dropping variables

    Hi there
    As you can see in my do file I have indicated which variables to drop

    Stata reads the command without an issue yet it is not reflected in the variable column where as you can see the variables haven't been dropped.

    Any ideas why?

    Click image for larger version

Name:	2.PNG
Views:	1
Size:	88.4 KB
ID:	1666535

  • #2
    Apparently all the code you show within your picture is contained within an if block (which you do not show us) similar to the following
    Code:
    if something==somethingelse {
    drop ...
    drop ...
    }
    and the if condition was not true, so all your drop commands were skipped, along with all your other commands.

    This is indicated by the fact that when Stata executes a command, the Results window shows a blank line between the command and the next command.

    Comment


    • #3
      I didnt understand this ….

      1. if stata didn’t understand the command / is not true would it not just stop running and come up with a red R error

      2. the shaded (in blue) is just defining a label as you can clearly see

      3.What is the solution to this?

      Comment


      • #4
        Martin:
        as far as your point 1. is concerned, if -if- caluse is not satisfied, Stata returns what follows:
        Code:
        . use "C:\Program Files\Stata17\ado\base\a\auto.dta"
        (1978 automobile data)
        
        . sum rep78
        
            Variable |        Obs        Mean    Std. dev.       Min        Max
        -------------+---------------------------------------------------------
               rep78 |         69    3.405797    .9899323          1          5
        
        . drop if rep78>1000 & rep78!=.
        (0 observations deleted)
        .
        2. what should be shaded in blue (that interested listers cannot see, as it is frequent with screenshots; this is one more reason why CODE delimiters are preferred as a way to share what you typed and what Stata gave you back, as recommended by the FAQ. Thanks) are -label. But you seem to be interested in -drop-ping variables, not -label-(s), isn't it?
        3. an excerpt/example of your dataset shared via -dataex- could help (more) positive replies. Thanks.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          In post #2 I wrote

          This is indicated by the fact that when Stata executes a command, the Results window shows a blank line between the command and the next command.
          This is not correct. If the code is in a block enclosed by braces, there will not be a blank line between commands.

          In post #3 you wrote

          if stata didn’t understand the command ... would it not just stop running and come up with a red R error
          This is correct.

          if ... the command / is not true would it not just stop running and come up with a red R error
          This is incorrect. You should review the documentation for the if command
          Code:
          help ifcmd
          which is different than the if qualifier
          Code:
          help if
          Please note that among the material scribbled over in blue in post #1 is the command
          Code:
          gsort -revisionreason
          but we see in the Data section of the Properties pane shown in your screenshot that the data is sorted by outcome. As I wrote in post #2, it appears that none of the code shown in post #1 was actually run by Stata: not the drop commands, not the gsort command, not the label commands.

          My point remains that the code you show was not actually run. Add the command
          Code:
          display "starting to drop"
          before the first drop command and you will see that the display command does not run.
          Code:
          clear all
          cls
          // read example data
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int(v1 v2 v3 v4 v5 v6 v7 v8)
          105 206 300 407 508 604 708 803
          101 200 303 409 502 606 701 800
          102 201 300 406 508 602 701 806
          103 200 303 409 508 605 701 807
          105 205 307 401 502 607 702 806
          end
          
          local group odd
          
          ds
          
          if "`group'"=="odd" {
              display "starting to drop"
              drop v1
              drop v3
              drop v5
              drop v7
          }
          if "`group'"=="even" {
              display "starting to drop"
              drop v2
              drop v4
              drop v6
              drop v8
          }
          
          ds
          Code:
          . local group odd
          
          .
          . ds
          v1  v2  v3  v4  v5  v6  v7  v8
          
          .
          . if "`group'"=="odd" {
          .     display "starting to drop"
          starting to drop
          .     drop v1
          .     drop v3
          .     drop v5
          .     drop v7
          . }
          
          . if "`group'"=="even" {
          .     display "starting to drop"
          .     drop v2
          .     drop v4
          .     drop v6
          .     drop v8
          . }
          
          .
          . ds
          v2  v4  v6  v8
          
          .
          Last edited by William Lisowski; 28 May 2022, 09:19.

          Comment


          • #6
            Ok.

            however, despite all your very helpful intuitive answers my question is still not answered. You’ve all explained and pointed out that stata has not run my command however I may have missed the point but why hasn’t stata dropped my variables ?

            1. is there an issue with the code ?

            Comment


            • #7
              why hasn’t stata dropped my variables ?
              Because it has not run your drop commands.

              is there an issue with the code ?
              There is clearly an issue with your code somewhere, but not in the code you have presented to us.

              Comment


              • #8
                Ok thanks for clarifying. I just wanted to see if there was an issue with my code presented in the screenshot as I couldn’t see the error myself.

                i’ll try go through the code once more and try troubleshoot

                many thanks.

                Comment


                • #9
                  [QUOTE=William Lisowski;n1666566]Apparently all the code you show within your picture is contained within an if block (which you do not show us) similar to the following
                  [CODE]

                  I am just rereading your replies. I know it’s difficult to tell however the block code scribbled in blue is actually a
                  label define code not an if code.

                  which I had no issue running

                  anyway i’ll try sort over the next few days and understand why it perhaps isn’t obeying

                  Comment


                  • #10
                    Apparently all the code you show within your picture is contained within an if block
                    I did not say "all the drop commands" - that includes the commands you scribbled out. The if block starts somewhere earlier.

                    I told you in post #2 that your code apparently contains an if block - commands surrounded by
                    Code:
                    if ...  {
                        some commands
                    }
                    and the condition on the if command was not true so none of the code was executed. Post 5 was devoted to correcting your assertion that if that were the case the code would stop running, and to show what an if block would look like,

                    So now back to the advice in post #2 and find the if command that's located somewhere before all the code in your screenshot, and see why it prevents the code you present from running.
                    Last edited by William Lisowski; 28 May 2022, 16:55.

                    Comment


                    • #11
                      Thank you so much William.

                      For my own personal knowledge, how did you know that there is an ‘if’ command prior to the screenshot ?

                      have you seen this issue happening before hand hence the reason you ask ?

                      and yes there is an ‘if’ command previous to this screenshot code . However I hadn’t realised the command didn’t work as I would have expected stata to stop and generate a R error.

                      Comment


                      • #12
                        I have not seen this issue happening before. I know how to program in Stata. I know that an if command will evaluate the logical expression and if it is false will skip over the block of code that follows it without executing the code. I learned that by reading the output of
                        Code:
                        help ifcmd
                        to which I referred you in post #4. Based on my programming knowledge and experience - not having seen a problem like this before - I concluded that an if command was the most likely reason that all of the commands you showed in post #1 had been skipped over without running them.

                        I expected that you wrote the code in your do-file that you are trying to run, but that appears not to be the case, since the do-file includes an if command and you didn't understand how the if command functions.

                        I'm sympathetic to you as a new user of Stata - there is quite a lot to absorb. And even worse if perhaps you are under pressure to produce some output quickly. Nevertheless, I'd like to encourage you to take a step back from your immediate tasks.

                        When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

                        The objective in doing the reading was not so much to master Stata - I'm still far from that goal - as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals.

                        Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

                        Stata also supples YouTube videos, if that's your thing.

                        Comment


                        • #13
                          Thank you for your very very kind explanation.

                          I will look for my do file and look into the If command vs If qualifier which another helpful user wrote about.

                          Comment


                          • #14
                            Hello all again.

                            I have given it some time to re address my issue.

                            I have gone through my code, and indepdently assessed each line of code to make sure all the IF commands work and replace the right number of values according to tab command.
                            I independenlty checked them and each IF command worked.

                            Going line by line I dropped the variables and it worked without issues.

                            However, when I select all my 700+ lines of code in bulk, STATA won't drop my selected variables.


                            Click image for larger version

Name:	Screenshot 2022-06-13 at 16.06.14.jpeg
Views:	1
Size:	316.6 KB
ID:	1669015

                            Click image for larger version

Name:	Screenshot 2022-06-13 at 16.06.38.jpeg
Views:	2
Size:	130.3 KB
ID:	1669017

                            Attached Files

                            Comment


                            • #15
                              Originally posted by Martin Imelda Borg View Post
                              I will look for my do file and look into the If command vs If qualifier ... .
                              What you show in post #14 are commands containing an "if qualifier" as described in the output of
                              Code:
                              help if
                              The "if qualifiers" are not the cause of your problem.

                              What I suggested in posts 2, 5, 7, 10 and 12 causes your problem is the presence an "if command" somewhere in your 700 lines of code, as described in the output of
                              Code:
                              help ifcmd
                              and as demonstrated in the example in post #5. The code you show does not contain any "if command", but some part of your code apparently has it.

                              Note also in post #14 that not only did your drop commands not run, but also the replace commands did not run - each command would have reported how many values were replaced. More evidence that there is an "if command" earlier in your code that is cause a block of code between "{" and "}" to be passed over without being run.

                              Perhaps you should upload your 700-line do-file as an attachment to your reply to this post and someone will be able to show you what you are not seeing. I am thinking that you are running code written by someone else which you do not fully understand,

                              Comment

                              Working...
                              X