Announcement

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

  • variable list

    I have some doubts about variable lists.
    I need to do three things:

    1) get the variables which are in one list but cannot be one of the variables of another list
    I tried as below and it worked

    Code:
    use ${ST}/ST_AC.dta, clear
    quietly describe, varlist
    local estab `r(varlist)'
    
    use ${PF}/PF_AC.dta, clear
    quietly describe, varlist
    local vars `r(varlist)'
    
    local new : list vars - estab
    ds `new'
    2) add two variable lists
    I tried as below but stata do not let be add, just subtract
    Code:
    use ${PF}/PF_AC.dta, clear
    quietly describe, varlist
    local vars `r(varlist)'
    local chave cnes competen
    
    local new : list vars + chave
    3) work with a variable list in global macro, once I have a variable list which I need all the time in my do file (and local lists just work if I run it together with other commands)
    I do not know how to do this.

    Thank you very much!

  • #2
    Also:
    4) How to find out the common variables between two different lists

    Comment


    • #3
      See help macrolists for an explanation of all the tools for manipulating macros containing lists.

      For (2) I think you may want something like
      Code:
      use ${PF}/PF_AC.dta, clear
      quietly describe, varlist
      local vars cnes competen `r(varlist)'
      // next command needed only if cnes and competen might already exist in the dataset
      local new : list uniq vars
      For (4) perhaps something like
      Code:
      list a_list & b_list
      I do not understand the problem in (3). Section 18.2 of the Stata User's Guide PDF has a good discussion of macros, comparing local and global macros.

      Comment


      • #4
        The problem is that those commands do not work if I run the commands separetely in my do file (what I aim to do)

        for example:
        if I run this:
        Code:
        use ${ST}/ST_AC.dta, clear
        quietly describe, varlist
        global estab `r(varlist)'
        and then, this:
        Code:
        use ${ST}/ST_AC.dta, clear
        quietly describe, varlist
        global vars `r(varlist)'
        
        global new : list vars - estab
        ds `new'
        the list estab is not recognized anymore. So the last command gives me the list -new- identical as the list -vars-

        Comment


        • #5
          In help extended_function we see
          Code:
          Macro extended functions for manipulating lists
                  list macrolist_directive
          and clicking on macrolist_directive takes us to help macrolists, where we see

          macnames that appear to the right of the colon are assumed to be the names of local macros. You may type local(macname) to emphasize that fact. Type global(macname) if you wish to refer to a global macro.
          So the last two lines of your code need to be changed to
          Code:
          global new : list global(vars) - global(estab)
          ds $new

          Comment

          Working...
          X