Announcement

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

  • Forvalues equal to the maximum

    Hi Stata Users

    I am using this code below and it is working very well:

    Code:
    bysort ID_com time: gen all=sum(v_local!=v_local[_n-1])
    forval j=1/8{
    egen v_local`j'=mean(cond(all==`j', v_local, .)), by (ID_com time)
    }
    and then I use this code which is also working very well:

    Code:
    gen byte trade_var= inlist(domestic,v_local1,v_local2,v_local3,v_local4,v_local5,v_local6,v_local7,v_local8)
    my question is about
    Code:
    forval j=1/10
    . In fact I do not know the exact number of "j"s. So I need to calculate first the maximum number of "j"s. The number could be 8, but also 10 or 12.
    I tried using
    Code:
    forval j=1/max`j'
    , which is obviously wrong and did not work. Is there any way to do that? And how would my second code change?

    Do I have to provide data example for this question?

    Thank you
    Max

  • #2
    For once, a data example isn't needed here.

    Code:
    su all, meanonly 
    
    forval j = 1/`r(max)' {
    should be enough of a hint, I hope.

    Comment


    • #3
      Dear Nick Cox,

      Thank you very much! It worked well.

      I would like to know if it could also work with the other code. Assuming that the maximum of "all" was 3:
      I have for example this code:
      Code:
      gen byte trade_var= inlist(domestic,v_local1,v_local2,v_local3)
      and this code;

      Code:
      gen trade_var2 =1 if domestic==v_local & (v_local==v_local1 & v_local==v_local2) | (v_local==v_local1 & v_local==v_local3) |(v_local==v_local2 & v_local==v_local3)
      they are both based on the code you have just helped me with.

      Thank you
      Max

      Comment


      • #4
        Sorry, I don't understand what you are seeking with #3.

        Comment


        • #5
          After I run the code using `r(max)' as mentioned above, I will get: v_local1, v_local2, v_local3, v_local_n..... Since I do not have access to the data, I do not know the exact number of v_local. In this code I assume just as an example that they are 3.

          Code:
          gen trade_var=1 if domestic == v_local1 | domestic == v_local2 | domestic == v_local3
          *or as I wrote above 
           gen byte trade_var= inlist(domestic,v_local1,v_local2,v_local3)
          My question is how I could also use `r(max)' in this code. and the other code mentioned in #3.

          Thanks

          Comment


          • #6
            Fair enough. You can store r(max) any way you wish


            Code:
            local max = r(max)
            
            scalar max = r(max)
            
            gen max = r(max)
            with varying longevity.

            You can also keep track of the variables created adding a line within the loop

            Code:
            local newvars `newvars'  v_local`j'
            Then outside the loop you have newvars containing

            Code:
            v_local1 v_local2 v_local3
            or whatever. If you need those names to be comma-separated, that is as simple as

            Code:
            local newvars_c : subinstr local newvars " " ",", all
            and benefit by

            Code:
             
             gen byte trade_var = inlist(domestic, `newvars_c')

            Comment


            • #7
              Dear Nick,

              Thank you very much for your help!

              Best wishes
              Max

              Comment


              • #8
                Sorry, I just had an issue with
                Code:
                 
                 gen byte trade_var = inlist(domestic, `newvars_c')
                Code:
                 
                 gen byte trade_var = inlist(domestic, `newvars_c') invalid syntax
                r(198);
                
                end of do-file

                Data example is as follows:
                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input int(year id_com v_local domestic)
                2005 19587 111 111
                2005 19587 111 112
                2005 19587 111 113
                2005 19587 111 114
                2005 19587 112 111
                2005 19587 112 112
                2005 19587 112 113
                2005 19587 112 114
                2005 19587 114 111
                2005 19587 114 112
                2005 19587 114 113
                2005 19587 114 114
                2005 19587 114 111
                2005 19587 114 112
                2005 19587 114 113
                2005 19587 114 114
                2005 19587 111 111
                2005 19587 111 112
                2005 19587 111 113
                2005 19587 111 114
                2005 19587 111 111
                2005 19587 111 112
                2005 19587 111 113
                2005 19587 111 114
                end

                Comment


                • #9
                  You need to put something like

                  Code:
                  macro list
                  or specifically

                  Code:
                  di "`newvars_c'"


                  before the command that fails to see what is being objected to.

                  One possibility is that you're forgetting that local macros have only local scope: see

                  SJ-20-2 dm0102 . . . . . . . . . Stata tip 138: Local macros have local scope
                  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
                  Q2/20 SJ 20(2):499--503 (no commands)
                  focuses on a common misunderstanding of how local macros work

                  if you have access.
                  Last edited by Nick Cox; 02 Nov 2022, 04:14.

                  Comment


                  • #10
                    The code works well! Thanks.
                    However, I guess the code is correct but what I need to generate was something different. Th three codes below, as I understood, should give the same results:
                    Code:
                     
                     *Code 1: gen trade_var=1 if domestic == v_local1 | domestic == v_local2 | domestic == v_local3 replace trade_var=0 if trade_var==. *Code 2:  gen byte trade_var= inlist(domestic,v_local1,v_local2,v_local3)
                    give:


                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input int year byte(id_com v_local domestic all v_local1 v_local2 v_local3 v_local4 trade_var)
                    2005 1 11 11 1 11 12 14 11 1
                    2005 1 11 12 1 11 12 14 11 1
                    2005 1 11 13 1 11 12 14 11 .
                    2005 1 11 14 1 11 12 14 11 1
                    2005 1 12 11 2 11 12 14 11 1
                    2005 1 12 12 2 11 12 14 11 1
                    2005 1 12 13 2 11 12 14 11 .
                    2005 1 12 14 2 11 12 14 11 1
                    2005 1 14 11 3 11 12 14 11 1
                    2005 1 14 12 3 11 12 14 11 1
                    2005 1 14 13 3 11 12 14 11 .
                    2005 1 14 14 3 11 12 14 11 1
                    2005 1 14 11 3 11 12 14 11 1
                    2005 1 14 12 3 11 12 14 11 1
                    2005 1 14 13 3 11 12 14 11 .
                    2005 1 14 14 3 11 12 14 11 1
                    2005 1 11 11 4 11 12 14 11 1
                    2005 1 11 12 4 11 12 14 11 1
                    2005 1 11 13 4 11 12 14 11 .
                    2005 1 11 14 4 11 12 14 11 1
                    end

                    Code:
                     
                     *Code 3:  gen byte trade_var = inlist(domestic, `newvars_c')
                    gives

                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input int year byte(id_com v_local domestic all v_local1 v_local2 v_local3 v_local4 trade_var)
                    2005 1 11 11 1 11 12 14 11 1
                    2005 1 11 12 1 11 12 14 11 0
                    2005 1 11 13 1 11 12 14 11 0
                    2005 1 11 14 1 11 12 14 11 0
                    2005 1 12 11 2 11 12 14 11 0
                    2005 1 12 12 2 11 12 14 11 1
                    2005 1 12 13 2 11 12 14 11 0
                    2005 1 12 14 2 11 12 14 11 0
                    2005 1 14 11 3 11 12 14 11 0
                    2005 1 14 12 3 11 12 14 11 0
                    2005 1 14 13 3 11 12 14 11 0
                    2005 1 14 14 3 11 12 14 11 1
                    2005 1 14 11 3 11 12 14 11 0
                    2005 1 14 12 3 11 12 14 11 0
                    2005 1 14 13 3 11 12 14 11 0
                    2005 1 14 14 3 11 12 14 11 1
                    2005 1 11 11 4 11 12 14 11 1
                    2005 1 11 12 4 11 12 14 11 0
                    2005 1 11 13 4 11 12 14 11 0
                    2005 1 11 14 4 11 12 14 11 0
                    end

                    Comment


                    • #11
                      I can't really comment on what else you might need.

                      Comment

                      Working...
                      X