Announcement

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

  • factor variable in syntax command

    Hi,

    I have an annoying issue when manipulating fixed effect in a syntax command.My program looks like that:

    Code:
    program define myprogram
    syntax [varlist] [using] [if] [pw aw fw], TREATment(varlist) [CONTrol(varlist fv)] 
    end
    I would like to be able to include in option "control" any kind of control variables, especially fixed effect. Yet when I use a string variable as a fixed effect, like in:

    Code:
    myprogram x y, treat(T) cont(i.country)
    if country is not a numerical variable I get the following message

    HTML Code:
    country:  string variables may not be used as factor variables
    (error in option control())

    while with xi option stata handles pretty well string variable fixed effect.

    Is there a way to let my control option accept string variable as fixed effect?

    Thanks

  • #2
    No, there isn't. Factor variable notation in Stata is only applicable to variables that are numeric and have non-negative integer values. However, you can convert your strong variable to such a numeric variable by using the -encode- command. E.g.

    Code:
    encode country, gen(ncountry)
    des country ncountry
    label list ncountry
    Then you an apply factor variable notation to ncountry.

    Comment


    • #3
      Thanks Clyde. Sure I can always transform my variable into a numerical but since I am writting a command I thought there would be a way to be flexible and accept both numeric and string option. Since xi: accepts string variable I thought it would make sense that an option in syntax would also accept string variables... I can't use namelist either because fixed effect are not accepted as namelist. Indeed if I tweak the command:
      program define myprogram syntax [varlist] [using] [if] [pw aw fw], TREATment(varlist) [CONTrol(namelist)] end I still get the error : i.country invalid name. I guess this is a limitation of the command syntax. Thanks anyway

      Comment


      • #4
        Namelist cannot be used in that way, and is described in the help for -syntax-. The prefix -xi- exists mainly to allow for backward compatibility with commands from before factor variable notation was introduced, I believe. In effect, Clyde's solution is much more expedient and consistent with modern Stata.

        Comment


        • #5
          Adrien,

          1) this is a standard feature of Stata, your command will not be the first one not to accept string variables for the fixed effects. Most Stata commands require you to transform manually your string variable into numerical beforehand. E.g., you cannot even -xtset- your data if the i index is a string:
          Code:
          . sysuse auto
          (1978 Automobile Data)
          
          . xtset make
          string variables not allowed in varlist;
          make is a string variable
          r(109);
          Another way to transform a string variable into numerical (and to me the standard way) is
          Code:
          egen numericalvar = group(stringvar), label
          2) What Clyde tells you is that you cannot use the factor variable facility to deal with fixed effects in a string variable, you firstly need to convert the string to numerical to be able to use the factor variable facility. Still you can allow as an option to your programme the fixed effect to be specified as a string, and then you deal with the fixed effect manually within your program. E.g., -areg- does that, in areg you can specify the fixed effect by a string variable.

          Comment


          • #6
            Ok thanks guys. Makes more sense now.
            Last edited by Adrien Bouguen; 05 Nov 2020, 17:30.

            Comment

            Working...
            X