Announcement

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

  • Generating variables using local macro

    Hello,

    I am trying to generate variables using a local macro. I am using household data in which each observation has an occupation ID. There are 298 different occupation IDs. I have reduced these to the 50 most common using the following code:

    Code:
    keep if _n<=50
    levelsof occ1950, local(top_occs)
    To develop a dummy for each of the top 50 occupations, I have done the following:

    Code:
    foreach occid of local top_occs {
          gen occ`occid'=(occ1950==`occid') 
          }
    Next, I want to generate a dummy for each occupation ID that interacts with a dummy variable for labor force participation. I have a dummy laborforce that takes a value of 1 if an observation is in the labor force. The following code results in an error for invalid syntax:

    Code:
    gen labf`occid'=(occ1950==`top_occs')*(labforce==1)
    I am not sure why this syntax is invalid. I thought that I had correctly defined every macro in this line of code. Perhaps I did this incorrectly. Thank you.

  • #2
    The local macro `occid' is the iterator of your -foreach- loop and it exists only inside that loop. If the command that is throwing error messages is outside of that loop, then `occid' is undefined there.

    Another common pitfall: if you are running this code line by line or in small blocks, you will get into trouble. A block of code that is highlighted and run from the do-editor is considered by Stata to be a program, and local macros defined therein are forgotten as soon as the block of code is finished executing. Later references to local macros in subsequent highlighted blocks of code yield the empty string. Whenever you are running chunks of code, you have to make sure that any macros referred to in the chunk are also defined in that chunk. If the definition of the local macro occurs much earlier in the code and much of the code in between is not relevant at the moment, you can comment out the intervening material.

    Added: All of that said, why are you doing this? This sounds like a colossal waste of time and effort on your part. If you need indicator ("dummy") variables and interaction terms for use in a regression or similar Stata command, use factor-variable notation instead. Calculating your own variables is error-prone and tedious, and it also will prevent you from using the wonderful -margins- command to interpret your regression output later. So read -help fvvarlist- and get started with factor-variable notation.

    Comment


    • #3
      Thank you for your detailed response. I understand that what I am doing may sound inefficient. I am doing it because, in addition to the dummy variable indicating the top 50 occupational IDs, I want to generate a dummy variable indicating occupational ID for observations that are in the labor force. Perhaps "interaction" wasn't the best term to use in this case.

      Comment


      • #4
        You can interact your dummy for in the labor force with the occupational id's - something like i.occup##i.employed . This will create separate dummies for each occup for employed and non-employed. However, if you only want to run the analysis on employed, then you need to include an if statement reg y i.occup if employed==1

        Comment

        Working...
        X