How to create the name of a macro programmatically and use it as a macro?












0















I am using the community-contributed command gvselect that permits to perform best subsets variable selection (a statistical method to select variables).



After I used it, I obtain the following result:



. return list

macros:
r(best3) : " q105capitalisationboursireouvalo q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
r(best2) : " q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
r(best1) : " q405existenceduncomitdesrmunrati"


Then I need to use one of these macros in another statistical estimation method.



For example, I can use the first one as follows:



 xtreg  logremglobale  `r(best3)' i.date, fe


(note the macro `r(best3)' is used as an argument)



My framework entails that I do not know in advance which macro I need to use, so I determine it programmatically.



In this simplified example I may have to use r(best1) , r(best2) or r(best3) (in reality there are more macros available) depending on the data.



All macros generated by gvselect have the same syntax: r(best + number + )



Once I have determined programmatically which macro I have to use (so for instance: number = 1) I'm stuck.



I don't know how to create the name of this macro programmatically and then use it as a normal macro. I'm not even sure if it is possible.



Could you help me with this ?



I have succeeded to replicate the macro name in a normal string as follows:



. scalar inum = 7

. gen macroname = "r(best" + string(inum,"%8.0g")+")"

. di macroname
r(best7)


But how can I use it in my estimation?



Obviously the following code doesn't work:



xtreg  logremglobale  `macroname' i.date, fe









share|improve this question





























    0















    I am using the community-contributed command gvselect that permits to perform best subsets variable selection (a statistical method to select variables).



    After I used it, I obtain the following result:



    . return list

    macros:
    r(best3) : " q105capitalisationboursireouvalo q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
    r(best2) : " q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
    r(best1) : " q405existenceduncomitdesrmunrati"


    Then I need to use one of these macros in another statistical estimation method.



    For example, I can use the first one as follows:



     xtreg  logremglobale  `r(best3)' i.date, fe


    (note the macro `r(best3)' is used as an argument)



    My framework entails that I do not know in advance which macro I need to use, so I determine it programmatically.



    In this simplified example I may have to use r(best1) , r(best2) or r(best3) (in reality there are more macros available) depending on the data.



    All macros generated by gvselect have the same syntax: r(best + number + )



    Once I have determined programmatically which macro I have to use (so for instance: number = 1) I'm stuck.



    I don't know how to create the name of this macro programmatically and then use it as a normal macro. I'm not even sure if it is possible.



    Could you help me with this ?



    I have succeeded to replicate the macro name in a normal string as follows:



    . scalar inum = 7

    . gen macroname = "r(best" + string(inum,"%8.0g")+")"

    . di macroname
    r(best7)


    But how can I use it in my estimation?



    Obviously the following code doesn't work:



    xtreg  logremglobale  `macroname' i.date, fe









    share|improve this question



























      0












      0








      0








      I am using the community-contributed command gvselect that permits to perform best subsets variable selection (a statistical method to select variables).



      After I used it, I obtain the following result:



      . return list

      macros:
      r(best3) : " q105capitalisationboursireouvalo q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
      r(best2) : " q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
      r(best1) : " q405existenceduncomitdesrmunrati"


      Then I need to use one of these macros in another statistical estimation method.



      For example, I can use the first one as follows:



       xtreg  logremglobale  `r(best3)' i.date, fe


      (note the macro `r(best3)' is used as an argument)



      My framework entails that I do not know in advance which macro I need to use, so I determine it programmatically.



      In this simplified example I may have to use r(best1) , r(best2) or r(best3) (in reality there are more macros available) depending on the data.



      All macros generated by gvselect have the same syntax: r(best + number + )



      Once I have determined programmatically which macro I have to use (so for instance: number = 1) I'm stuck.



      I don't know how to create the name of this macro programmatically and then use it as a normal macro. I'm not even sure if it is possible.



      Could you help me with this ?



      I have succeeded to replicate the macro name in a normal string as follows:



      . scalar inum = 7

      . gen macroname = "r(best" + string(inum,"%8.0g")+")"

      . di macroname
      r(best7)


      But how can I use it in my estimation?



      Obviously the following code doesn't work:



      xtreg  logremglobale  `macroname' i.date, fe









      share|improve this question
















      I am using the community-contributed command gvselect that permits to perform best subsets variable selection (a statistical method to select variables).



      After I used it, I obtain the following result:



      . return list

      macros:
      r(best3) : " q105capitalisationboursireouvalo q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
      r(best2) : " q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
      r(best1) : " q405existenceduncomitdesrmunrati"


      Then I need to use one of these macros in another statistical estimation method.



      For example, I can use the first one as follows:



       xtreg  logremglobale  `r(best3)' i.date, fe


      (note the macro `r(best3)' is used as an argument)



      My framework entails that I do not know in advance which macro I need to use, so I determine it programmatically.



      In this simplified example I may have to use r(best1) , r(best2) or r(best3) (in reality there are more macros available) depending on the data.



      All macros generated by gvselect have the same syntax: r(best + number + )



      Once I have determined programmatically which macro I have to use (so for instance: number = 1) I'm stuck.



      I don't know how to create the name of this macro programmatically and then use it as a normal macro. I'm not even sure if it is possible.



      Could you help me with this ?



      I have succeeded to replicate the macro name in a normal string as follows:



      . scalar inum = 7

      . gen macroname = "r(best" + string(inum,"%8.0g")+")"

      . di macroname
      r(best7)


      But how can I use it in my estimation?



      Obviously the following code doesn't work:



      xtreg  logremglobale  `macroname' i.date, fe






      stata stata-macros






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 23:01









      Pearly Spencer

      10k173360




      10k173360










      asked Nov 19 '18 at 21:57









      MalickMalick

      2,37011827




      2,37011827
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The following works for me:



          sysuse auto, clear

          gvselect <term> weight trunk length, nmodels(2): regress mpg <term> i.foreign

          return list

          scalars:
          r(nmodels) = 2
          r(k) = 3

          macros:
          r(best31) : " weight trunk length"
          r(best22) : " weight trunk"
          r(best21) : " weight length"
          r(best12) : " length"
          r(best11) : " weight"

          matrices:
          r(info) : 5 x 4

          scalar inum = 31
          local macroname r(best`= inum')

          regress price ``macroname''

          Source | SS df MS Number of obs = 74
          -------------+---------------------------------- F(3, 70) = 12.47
          Model | 221230614 3 73743538 Prob > F = 0.0000
          Residual | 413834782 70 5911925.46 R-squared = 0.3484
          -------------+---------------------------------- Adj R-squared = 0.3204
          Total | 635065396 73 8699525.97 Root MSE = 2431.4

          ------------------------------------------------------------------------------
          price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
          -------------+----------------------------------------------------------------
          weight | 4.721599 1.132265 4.17 0.000 2.463369 6.979829
          trunk | 28.37644 97.05843 0.29 0.771 -165.2005 221.9534
          length | -102.6652 42.58687 -2.41 0.019 -187.602 -17.72834
          _cons | 10812.33 4574.211 2.36 0.021 1689.353 19935.3
          ------------------------------------------------------------------------------





          share|improve this answer
























          • Perfect, and thank you for the detailed answer. I didn't know it was possible to create a double enclosure for a macro.

            – Malick
            Nov 19 '18 at 22:33













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383223%2fhow-to-create-the-name-of-a-macro-programmatically-and-use-it-as-a-macro%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The following works for me:



          sysuse auto, clear

          gvselect <term> weight trunk length, nmodels(2): regress mpg <term> i.foreign

          return list

          scalars:
          r(nmodels) = 2
          r(k) = 3

          macros:
          r(best31) : " weight trunk length"
          r(best22) : " weight trunk"
          r(best21) : " weight length"
          r(best12) : " length"
          r(best11) : " weight"

          matrices:
          r(info) : 5 x 4

          scalar inum = 31
          local macroname r(best`= inum')

          regress price ``macroname''

          Source | SS df MS Number of obs = 74
          -------------+---------------------------------- F(3, 70) = 12.47
          Model | 221230614 3 73743538 Prob > F = 0.0000
          Residual | 413834782 70 5911925.46 R-squared = 0.3484
          -------------+---------------------------------- Adj R-squared = 0.3204
          Total | 635065396 73 8699525.97 Root MSE = 2431.4

          ------------------------------------------------------------------------------
          price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
          -------------+----------------------------------------------------------------
          weight | 4.721599 1.132265 4.17 0.000 2.463369 6.979829
          trunk | 28.37644 97.05843 0.29 0.771 -165.2005 221.9534
          length | -102.6652 42.58687 -2.41 0.019 -187.602 -17.72834
          _cons | 10812.33 4574.211 2.36 0.021 1689.353 19935.3
          ------------------------------------------------------------------------------





          share|improve this answer
























          • Perfect, and thank you for the detailed answer. I didn't know it was possible to create a double enclosure for a macro.

            – Malick
            Nov 19 '18 at 22:33


















          1














          The following works for me:



          sysuse auto, clear

          gvselect <term> weight trunk length, nmodels(2): regress mpg <term> i.foreign

          return list

          scalars:
          r(nmodels) = 2
          r(k) = 3

          macros:
          r(best31) : " weight trunk length"
          r(best22) : " weight trunk"
          r(best21) : " weight length"
          r(best12) : " length"
          r(best11) : " weight"

          matrices:
          r(info) : 5 x 4

          scalar inum = 31
          local macroname r(best`= inum')

          regress price ``macroname''

          Source | SS df MS Number of obs = 74
          -------------+---------------------------------- F(3, 70) = 12.47
          Model | 221230614 3 73743538 Prob > F = 0.0000
          Residual | 413834782 70 5911925.46 R-squared = 0.3484
          -------------+---------------------------------- Adj R-squared = 0.3204
          Total | 635065396 73 8699525.97 Root MSE = 2431.4

          ------------------------------------------------------------------------------
          price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
          -------------+----------------------------------------------------------------
          weight | 4.721599 1.132265 4.17 0.000 2.463369 6.979829
          trunk | 28.37644 97.05843 0.29 0.771 -165.2005 221.9534
          length | -102.6652 42.58687 -2.41 0.019 -187.602 -17.72834
          _cons | 10812.33 4574.211 2.36 0.021 1689.353 19935.3
          ------------------------------------------------------------------------------





          share|improve this answer
























          • Perfect, and thank you for the detailed answer. I didn't know it was possible to create a double enclosure for a macro.

            – Malick
            Nov 19 '18 at 22:33
















          1












          1








          1







          The following works for me:



          sysuse auto, clear

          gvselect <term> weight trunk length, nmodels(2): regress mpg <term> i.foreign

          return list

          scalars:
          r(nmodels) = 2
          r(k) = 3

          macros:
          r(best31) : " weight trunk length"
          r(best22) : " weight trunk"
          r(best21) : " weight length"
          r(best12) : " length"
          r(best11) : " weight"

          matrices:
          r(info) : 5 x 4

          scalar inum = 31
          local macroname r(best`= inum')

          regress price ``macroname''

          Source | SS df MS Number of obs = 74
          -------------+---------------------------------- F(3, 70) = 12.47
          Model | 221230614 3 73743538 Prob > F = 0.0000
          Residual | 413834782 70 5911925.46 R-squared = 0.3484
          -------------+---------------------------------- Adj R-squared = 0.3204
          Total | 635065396 73 8699525.97 Root MSE = 2431.4

          ------------------------------------------------------------------------------
          price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
          -------------+----------------------------------------------------------------
          weight | 4.721599 1.132265 4.17 0.000 2.463369 6.979829
          trunk | 28.37644 97.05843 0.29 0.771 -165.2005 221.9534
          length | -102.6652 42.58687 -2.41 0.019 -187.602 -17.72834
          _cons | 10812.33 4574.211 2.36 0.021 1689.353 19935.3
          ------------------------------------------------------------------------------





          share|improve this answer













          The following works for me:



          sysuse auto, clear

          gvselect <term> weight trunk length, nmodels(2): regress mpg <term> i.foreign

          return list

          scalars:
          r(nmodels) = 2
          r(k) = 3

          macros:
          r(best31) : " weight trunk length"
          r(best22) : " weight trunk"
          r(best21) : " weight length"
          r(best12) : " length"
          r(best11) : " weight"

          matrices:
          r(info) : 5 x 4

          scalar inum = 31
          local macroname r(best`= inum')

          regress price ``macroname''

          Source | SS df MS Number of obs = 74
          -------------+---------------------------------- F(3, 70) = 12.47
          Model | 221230614 3 73743538 Prob > F = 0.0000
          Residual | 413834782 70 5911925.46 R-squared = 0.3484
          -------------+---------------------------------- Adj R-squared = 0.3204
          Total | 635065396 73 8699525.97 Root MSE = 2431.4

          ------------------------------------------------------------------------------
          price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
          -------------+----------------------------------------------------------------
          weight | 4.721599 1.132265 4.17 0.000 2.463369 6.979829
          trunk | 28.37644 97.05843 0.29 0.771 -165.2005 221.9534
          length | -102.6652 42.58687 -2.41 0.019 -187.602 -17.72834
          _cons | 10812.33 4574.211 2.36 0.021 1689.353 19935.3
          ------------------------------------------------------------------------------






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 22:19









          Pearly SpencerPearly Spencer

          10k173360




          10k173360













          • Perfect, and thank you for the detailed answer. I didn't know it was possible to create a double enclosure for a macro.

            – Malick
            Nov 19 '18 at 22:33





















          • Perfect, and thank you for the detailed answer. I didn't know it was possible to create a double enclosure for a macro.

            – Malick
            Nov 19 '18 at 22:33



















          Perfect, and thank you for the detailed answer. I didn't know it was possible to create a double enclosure for a macro.

          – Malick
          Nov 19 '18 at 22:33







          Perfect, and thank you for the detailed answer. I didn't know it was possible to create a double enclosure for a macro.

          – Malick
          Nov 19 '18 at 22:33




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383223%2fhow-to-create-the-name-of-a-macro-programmatically-and-use-it-as-a-macro%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]