Announcement

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

  • Creating local macro with variable names where each variable contains a given phrase

    Colleagues,

    How I can quickly create a local macro containing variable names for each of the string variable that contains a given phrase?
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    findname (Cox, SJ) is one way.

    Code:
    findname ,any(strmatch(@, "<myphrase>"))
    local mylocal `r(varlist)'
    Best
    Daniel

    Comment


    • #3
      findname from the Stata Journal (search findname) offers one solution here.

      Code:
       
      . clear
      
      . set obs 3
      obs was 0, now 3
      
      . gen a = "Stata"
      
      . gen b = "R"
      
      . gen c = cond(_n == 1, "SAS", "Stata")
      
      . findname, any(strpos(@, "Stata"))
      a  c
      
      . findname, any(strpos(@, "Stata")) local(wanted)
      a  c
      
      . di "`wanted'"
      a c

      Comment


      • #4
        Thanks, in the meanwhile it also occurred to me that findval (SSC) can do this.

        Code:
        // Find the variables with the string
        findval Good, substr
            
        // Encode
        foreach var of varlist `r(vars)' {
            encode `var', gen(`var'2)
            local labels `"`labels' `var'2 "'
        }
        
        di "`labels'"
        
        // Combine created value labels
        labvalpool lvllbl `labels'
        Kind regards,
        Konrad
        Version: Stata/IC 13.1

        Comment


        • #5
          One thing that is not clear to me, is why I'm loosing my `r(vars)' macro values in the code below.
          Code:
          . do "C:\file.do"
          
          . // Import the data
          . import excel ///
          >         "C:\file.xlsx firstrow clear
          
          .
          . // Find the variables with the string
          . findval Good, substr
          The value of Good is found in variables:  list of variables as it should be
          
          .         
          . di "`r(vars)'"
          list of values labels as it should be
          
          .
          . // Encode
          . foreach var of varlist `r(vars)' {
            2.         encode `var', gen(`var'2)
            3.         local labels `"`labels' `var'2 "'
            4. }
          
          . di "`r(vars)'"
          list of variables as it should be
          
          . di "`labels'"
          list of values labels as it should be
          
          .
          . // Combine created value labels
          . labvalpool lvllbl `labels'
          
          .
          . di "`r(vars)'"
          
          nothing shows
          .
          . // Drop redundant labels
          . label drop `labels'
          
          . // Assign value labels
          . foreach var of varlist `r(vars)' {
            2.         label values `var' lvllbl
            3. }
          varlist required - understandably as `r(vars)' is gone
          r(100);
          
          end of do-file
          
          r(100);
          
          .
          Kind regards,
          Konrad
          Version: Stata/IC 13.1

          Comment


          • #6
            labvalpool (part of labutil2, SSC) clears the r() results. This is no bug, as one might suspect.* Running tabulate for example, will also clear previous r() results and so will lots of other commands.

            If you want to use such results more than once, you should pass them to a local (or global), as in

            Code:
            somecommand
            local result `r(resultname)'

            Best
            Daniel

            * One could argue that since labvalpool does not return anything in r() it should prevent previous contents. I have recently started to pay some attention to this issue. I do not know whether there exist official Stata commands that do not return results and still clear previous results.

            Comment


            • #7
              Daniel's post mentioning findname and mine were sent at about the same time. Note that findname has a local() option whereby a local macro can be created in the calling program's space, so an extra step of copying returned results to a local can be avoided.

              This is linked to Konrad's new question. labvalpool (in turn Daniel's program from SSC, as you are asked to explain) internally runs label list, which is an r-class command and so one that zaps previous r-class results.

              There is an advantage therefore in running a command leaving local macros in its wake which you can control. Otherwise you may need to copy r-class results immediately into a local macro in order that they not be lost.


              Last edited by Nick Cox; 25 Nov 2014, 05:51.

              Comment


              • #8
                Originally posted by daniel klein View Post
                labvalpool (part of labutil2, SSC) clears the r() results. This is no bug, as one might suspect.* Running tabulate for example, will also clear previous r() results and so will lots of other commands.
                Thanks, after calling di command after each section it occurred to me that this must be the case. Initially, I simply passed the results to another local macro. Later I realised that I'm actually interested in applying last command to the list of new variables, which is equivalent to what is stored in the "labels" macro so I used that.
                Kind regards,
                Konrad
                Version: Stata/IC 13.1

                Comment


                • #9
                  Twice today Daniel and I posted similar answers within 2 minutes!

                  Saved results, specifically here r-class results, are so useful that the price of being obliged to save them if and when you want them is fair. Perhaps being able to protect r-class results from being overwritten should be a user request as offhand I can't see a way of doing that.
                  Last edited by Nick Cox; 25 Nov 2014, 11:21.

                  Comment


                  • #10
                    Well one could of course just go

                    Code:
                    tempname Rresults
                    _return hold `Rresults'
                    commd_clearing_r()
                    _return restore `Rresults'
                    As mentioned, I recently started implementing this in my programs if the programs do not return something in r() on purpose ().the returned results Conrad mentions, are a side-effect, and Nick already explained where it comes from).

                    Best
                    Daniel

                    Comment


                    • #11
                      I didn't know, or had forgotten, about _return, so thanks for that mention.

                      Comment

                      Working...
                      X