Announcement

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

  • egen with macro lists in a loop

    Hi All,

    I am hoping to get some help with the following if possible. This code does not work and I get the error message "varlist not allowed".
    I am trying to automate creation of variables that is done often. In this case I am categorising a count variable based on below the mean and above the mean and then it is labelled.

    Basically the macro lists just needs to be adapted and then the code below in the loops runs accordingly.

    My checks tells me that it breaks down at the very first line in the 2nd loop - egen vmax = max(`prfx'`var') + 1 but I am pretty sure the other lines are suspect too probably for the same reason - perhaps the way I have concatenated the two macros.

    label def Cat_X_scale 0 "low values" 1 "high values"

    local var_list "var_name" /* var_name is a count */
    local prfx_list "x y z"
    local newvar "var_name_cat"
    local varlab "Categorised - scale"
    local varval "Cat_X_scale"

    foreach prfx in `prfx_list'{


    foreach var in `var_list'{

    egen vmax = max(`prfx'`var') + 1

    local vmax2 = vmax

    egen vmean = mean(`prfx'`var')

    local vmean2 = ceil(vmean)

    egen `prfx'`newvar' = cut(`prfx'`var'), at(0 `vmean2' `vmax2') icodes

    label var `prfx'`newvar' `varlab'

    label val `prfx'`newvar' `varval'

    display " this is a check for `prfx'`newvar' & `prfx'`var' "
    table `prfx'`newvar', contents(min `prfx'`var' max `prfx'`var')

    drop vmax vmean
    }
    }

    Any help would be appreciated and thank you in advance,

    Don

  • #2
    Do the variables xvar_name, yvar_name, and zvar_name already exist in your dataset?
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Yes, xvar_name, yvar_name, and zvar_name already exist in the data set. The prefix indicates time points of measurements.

      Thanks Carole.

      Don

      Comment


      • #4
        In general, set trace on is your friend for debugging. Sometimes it is hard to find the error and helps to set the trace depth to 1 or 2. Even then, you will likely have to scroll through some output to locate the error. See help trace. Also, it is helpful if you post a sample of your data using -dataex- (please read FAQ 12.2). I randomly generated some data, but it is unlikely to mirror your specific dataset and may cause or not show errors that you encounter.

        The max() function of egen does not allow additional arguments, so
        Code:
        egen foo=max(bar)
        is legal, but
        Code:
        egen foo=max(bar) + 1
        is not.

        Also, you need commas in the -at()- option of the -cut- function:
        Code:
        egen `prfx'`newvar' = cut(`prfx'`var'), at(0 ,`vmean2' ,`vmax2') icodes
        You also need quotation marks around the labels:
        Code:
        label var `prfx'`newvar' "`varlab'"
        label val `prfx'`newvar' "`varval'"
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Thanks so much Carole - your code works perfectly.

          I did not notice in the egen help file that the max() function of egen does not allow additional arguments.
          Where do you find that?

          Also I found that commas were not needed in at(0 ,`vmean2' ,`vmax2')

          Many regards,

          Don

          Comment


          • #6
            Hi Don, I should have clearer: The max() function of egen does not allow additional arguments outside the parentheses, so the following is legal
            Code:
            egen foo=max(bar +1)
            but
            Code:
             egen foo=max(bar)+1
            is not.

            The help file shows an expression inside the parentheses.
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              Right - thanks again Carole.

              Don

              Comment

              Working...
              X