Announcement

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

  • Gen variable for number of vaccine doses at a specific date

    What I would like to generate is a variable for all individuals that represents how many vaccine doses they have had by a specific date (the date they answered a survey)

    I have date on vaccine doses. Dose 1, Doses 2 etc.
    I have the dates of these doses. This is different for each individual.
    I have the date that they answered a survey. This is different for each individual.

    Can anyone help as to how to generate this new variable which would tell me on the date of survey response how many doses of vaccine they had using the above data.

    Thanks in advance.


  • #2
    Adam:
    the following toy-example springs to my mind when reading your post:
    Code:
    . set obs 3
    Number of observations (_N) was 0, now 3.
    
    . g id=1
    
    . g vaccine_date=_n
    
    . g vaccine_dose=1
    
    . g survey_date=1 in 3
    
    . replace survey_date=0 if survey_date==.
    
    . bysort id: egen wanted=sum(vaccine_dose) if survey_date<=1
    
    . replace wanted=0 if survey_date!=1
    
    . list
    
         +----------------------------------------------+
         | id   vaccin~e   vaccine~e   survey~e   wanted |
         |----------------------------------------------|
      1. |  1          1          1          0        0 |
      2. |  1          2          1          0        0 |
      3. |  1          3          1          1        3 |
         +----------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Just off the top of my head, I can think of at least three different data organizations that would match your description, and each of them would require different code to do what you ask. I don't think anybody can help you until you post example data. And the only truly helpful way to do that is with the -dataex- command.

      If you are running version 18, 17, 16 or a fully updated version 15.1 or 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.

      Added: Crossed with #2. If your data are organized in the same way as Carlo's example, his solution will do the job for you. If they are not....
      Last edited by Clyde Schechter; 29 Sep 2023, 12:05.

      Comment

      Working...
      X