Announcement

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

  • get a list of variables, except five varibles that might be in the list

    I would like to get the list of all variables except five variables that might be in my dataset. When one of them are not, I get an error. So I used the capture command, but the problem is that I have to do all combinations of those five variables being in my list. To ilustrate it, imagine I want to get the list of all varibles, except two, I would have to go:

    capture noisily confirm variable var1
    if !_rc {
    capture noisily confirm variable var2
    if !_rc {
    ds var1 var2, not
    }
    ds var1, not
    }
    else {
    capture capture noisily confirm variable var2
    if !_rc {
    ds var2, not
    else {
    ds
    }

    Imagine to it for five variables, instead of two! It would me mad!
    Please someone help me!

  • #2
    The following code demonstrates an approach using macro variables and one of the more interesting macro functions (see help macrolists for details).
    Code:
    // make some variables
    clear
    set obs 1
    gen a = 1
    gen b = 2
    // run the example
    quietly describe, varlist
    local vars `r(varlist)'
    local omit b c
    local want : list vars - omit
    display "`want'"
    Last edited by William Lisowski; 12 Jun 2015, 18:20.

    Comment


    • #3
      Wow, perfect!

      Comment


      • #4
        I have just checked that the * doesn't work in this case.
        ex: If I want to omit variables diag1 and diag2 (of course for this example I would have to problem in writting diag1 and diag2, but my dataset has a lot of cases like that)

        quietly describe, varlist local vars `r(varlist)' local omit diag* local want : list vars - omit display "`want'" --> it still shows diag1 diag2

        Comment


        • #5
          I don't know why when I try to write a code in the grey area, it doesn't jump lines (it does for me but not when I send it)
          Here below, jumping lines:

          quietly describe,
          varlist local vars `r(varlist)'
          local omit diag*
          local want : list vars - omit
          display "`want'" --> it still shows diag1 diag2

          Comment


          • #6
            Paula:: Please pay attention to laying out your code using CODE delimiters as detailed in the FAQ Advice. It takes about 2 minutes to learn once, and each time you use it you save each reader some strain (and each time you don't use it you may drive away other readers). Here is your code presented as is requested. I've fixed an incorrect line-break.

            % begin original. edited

            Code:
            quietly describe, varlist
            local vars `r(varlist)'  
            local omit diag* 
            local want : list vars - omit
            display "`want'"
            It still shows

            Code:
            diag1 diag2


            % end original. edited

            The problem with this code is that the macro directive is literal and does not expand wildcard varlists. Nor did WIlliam claim that would work.

            You need something more like this


            Code:
            quietly describe, varlist
            local vars `r(varlist)'  
            unab omit:  diag* 
            local want : list vars - omit
            display "`want'"
            except that

            Code:
            ds diag*, not
            display "`r(varlist)'"
            seems more direct.
            Last edited by Nick Cox; 13 Jun 2015, 11:08.

            Comment


            • #7
              Thank you for stepping in, Nick. I too suggest that Paula take the time to read the FAQ linked to at the top of this page.

              Some background on my example. In the original posting in this subject, the example is clearly trying to work around the problem that
              Code:
              ds var1 var2, not
              fails if either var1 or var2 doesn't exist. It also turns out that
              Code:
              ds var*, not
              fails if no variable is matched - in this example, if no variable begins "var". The same is apparently true for unab.

              The problem is that the arguments to ds and unab need to form a Stata "variable list". What is provided in this case, though, is a list of possible variables, some (or all?) of which may not be variables in the current data. As a result, under certain circumstances, ds or unab will fail.

              Since the subject of this topic is
              get a list of variables, except five varibles that might be in the list
              I went for an approach that (I hoped) would work in all cases by avoiding treating the list as a Stata variable list. Having done that, I thought that typing, for example,
              Code:
              omit diag1 diag2 diag3 diag4 diag5
              is in keeping with my example and is not excessive typing, compared to the requirements of the example in the original posting.

              Comment


              • #8
                Perfect, William. That's exactly the problem! I will write them all.
                Sorry about my copied codes. I have just read how to put it in the grey area.
                Thanks very much for your time.

                Comment


                • #9
                  Suppose there's a command without an auxiliary help file. And if I forget the command's syntax, how can I insert a piece of code into its .ado file, and let Stata automatically display its syntax as an error message? Will the -capture- and -confirm- play a role in this case?
                  Code:
                  program define foobar
                  syntax varlist, by(varname) [STats(string) anyotheroptions(...)]
                  ...
                  end
                  If I totally forget foobar's syntax, Stata will caution me with a return code 100:
                  Code:
                  foobar
                  varlist required
                  r(100);
                  And can I and how to get a caution like below? Is it possible?
                  Code:
                  foobar
                  syntax varlist, by(varname) [STats(string) anyotheroptions(...)]
                  Last edited by Chen Samulsion; 09 Jan 2023, 03:50.

                  Comment


                  • #10
                    Code:
                    program define foobar
                    capture syntax varlist, by(varname) [STats(string) anyotheroptions(...)]
                    if _rc {
                        di "syntax is <fill it here>"
                        exit 198
                    }
                    end

                    EDIT #9 does not seem to have any relation to the title or contents of the thread.
                    Last edited by Nick Cox; 09 Jan 2023, 04:36.

                    Comment


                    • #11
                      Thank you very much Nick, it's great!

                      Comment

                      Working...
                      X