Announcement

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

  • Generate a New Variable: if any

    I have a variable called DocVisit that records the number of times a respondent visits a clinic. It takes the value of: 1, 2, 3, 4, 5, 6, 7, 8, 9 10,

    I want to create a new variable that records the number of times a respondent visits a doctor:
    -- less than 5 times per month (DocVisit=1
    -- more than 5 month(DocVisit=2)
    -- sometimes less than 5x month, sometimes more than 5x per month (DocVisit=3)

    I am using this syntax, but it is giving me only two values:

    Code:
    gen DocVisit_3=.
    replace DocVisit_3=1 if DocVisit<=5
    replace DocVisit_3=2 if Docvisot>=5
    replace DocVisit_3=3 if inlist(DocVisit, 1,2,3,4,6,7,8,9,10)
    fre DocVisit_3

    I will appreciate some assistance with this. Thansk cY

  • #2
    I'm going to speculate that you have another variable that identifies patients, let's call it id, and that each patient has multiple observations in your data set. I'm also going to speculate that what you mean is that you want the variable to distinguish patients who always have DocVisit <=5, from patients who always have DocVisit >5, and from those who sometimes have DocVisit <= 5 and sometimes >5.

    Code:
    gen byte dv_5 = DocVisit <= 5
    by id (dv_5), sort: gen DocVisit_3 = 1 if dv_5[1] == 1
    by id (dv_5): replace DocVisit_3 = 2 if dv_5[_N] == 0
    replace DocVisit_3 = 3 if missing(DocVisit_3)
    I would normally not make so many speculations and offer an answer: I would usually ask that you post back with an example of your data and a clarification of your question. But as I am going to go offline shortly, I didn't want to put you to the trouble and then not have you get a prompt response. If this is not what you want, please post back.

    But in the future (including posting back on this thread, if you need to), whenever you want help with coding something, always show example data, and always use the -dataex- command to do that. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment

    Working...
    X