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

  • Generating a new variable conditional on multiple values - using or command?

    I Hope someone can help me with this:

    I have a dataset of households in various zip codes. I want to generate a variable that places the hholds into five different categories according to an area index of multiple deprivation using the zip codes that were correspondingly identified.

    The logical way to do this in stata is to write a command that looks like this:

    gen if aimd5=="12345" | zip=="82456" | zip=="56234" | zip==..............
    replace if aimd5=="75687" | zip=="45688" | zip=="95689" | zip==..............
    replace if aimd5=="14687" | zip=="34687" | zip=="64687" | zip==..............
    replace if aimd5=="54687" | zip=="54645" | zip=="54687" | zip==..............
    replace if aimd5=="64687" | zip=="78987" | zip=="21387" | zip==..............

    There would be about 8000 "or" conditions The problem is, Stata doesn't allow this many "OR" conditions in that statement.
    This command works fine if I only put in 10 zips or so.

    Is there any way for me to perform this operation?
    Kind regards
    Last edited by Emmanuel Claudo; 21 Feb 2020, 07:16.

  • #2
    Welcome to Statalist.

    You have accidentally posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language, which is different than Stata's command language, and different than Stata's matrix commands. Your question will see a much larger audience if you post it in Statalist's General Forum.

    Also, if you have not already done so, take a look at the Statalist FAQ linked to at the top of this page for posting guidelines and suggestions.

    With that said, the logical way to create your new variable is to read your list of 8000 ZIP Codes and their corresponding index numbers into a new Stata dataset and then use the merge command to combine the observations in it with the observations in your household dataset, matching on the value of zip. See the output of help merge for more details, and if you have followup questions, best to start a new topic in the General Forum, where others will see it.