Announcement

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

  • Creating a new variable (that changes value in every 3 days....)

    I am working with survey dataset. It has 30 people (variable name = dlp, numeric) who start work in different date (variable name= date, data type=date). I would like to create a new variable "work" which reflects the fact that each of the worker complete the work in every 3 days ( represented by Round 1), then continue the same work for next three days (represented by Round 2). And after every 6 days, they take 1 day off. Before they continue with Round 3 work. And this trend is likely to continue till december 2022.

  • #2
    Your question really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what you have shared. Please help us help you. Show example data, as you did in your previous topic. Tell us what values you would expect the new variable.

    The Statalist FAQ provides advice on effectively posing your questions, posting data, and sharing Stata output. In particular, when asking for help creating new variables, always show example data, and when showing example data, always present it with dataex.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Comment


    • #3
      Thank you for the suggestion William.... Here is the output generated by using dataex

      dataex NameoftheStudent Date dlp in 50/100

      ----------------------- copy starting from the next line -----------------------
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str25 NameoftheStudent int Date float dlp
      "Binita Bk"        22670 1
      "Binita Bk"        22675 1
      "Binita Bk"        22697 1
      "Binita Bk"        22679 1
      "Binita Bk"        22699 1
      "Bipin Giri"       22668 1
      "Bipin Giri"       22676 1
      "Bipin Giri"       22671 1
      "Bipin Giri"       22673 1
      "Bipin giri"       22684 1
      "Bipin giri"       22686 1
      "Bipin giri"       22695 1
      "Bipin giri"       22697 1
      "Bipin giri"       22689 1
      "Bipin giri"       22680 1
      "Deepa nepali "    22665 1
      "Deepesh Oli"      22670 1
      "Deepesh Oli"      22677 1
      "Deepesh Oli"      22673 1
      "Deepesh oli"      22686 1
      "Deepesh oli"      22680 1
      "Deepesh oli"      22666 1
      "Deepesh oli "     22691 1
      "Dipesh oli"       22684 1
      "Dipsika Pun"      22677 1
      "Dipsika pun"      22690 1
      "Dipsika pun"      22693 1
      "Dipsika pun"      22669 1
      "Dipsika pun"      22665 1
      "Dipsika pun"      22672 1
      "Dipsika pun"      22697 1
      "Dipsika pun"      22686 1
      "Dipsika pun"      22680 1
      "Dipsika pun"      22700 1
      "Dipsika pun
      "     22684 1
      "Dipsika pun "     22665 1
      "Laxman khadka "   22668 1
      "Lila Rawal"       22669 1
      "Lokendra  kathri" 22687 1
      "Lokendra Katheri" 22669 1
      "Lokendra Katheri" 22666 1
      "Lokendra Kathri"  22672 1
      "Lokendra kathri"  22693 1
      "Lokendra kathri"  22699 1
      "Lokendra kathri"  22675 1
      "Lokendra kathri"  22690 1
      "Lokendra kathri"  22684 1
      "Lokendra kathri"  22677 1
      "Milan Gm"         22692 1
      "Milan Gm"         22668 1
      "Milan Gm"         22684 1
      end
      format %td Date
      ------------------ copy up to and including the previous line ------------------

      Listed 51 out of 3169 observations

      Last edited by Santosh Poudel; 27 Feb 2022, 20:49.

      Comment


      • #4
        I created "dlp" variable to represent a worker (they are 30 in total). In one day, a worker call 3-4 students (represented by "NameoftheStudent"). Each worker would call their students 2 times from Sunday - Friday. A worker usually do not have to call on Saturdays if they have already called all of his/her students twice in that week. This cycle repeats weekly.

        Comment


        • #5
          Each worker would call their students 2 times from Sunday - Friday. A worker usually do not have to call on Saturdays if they have already called all of his/her students twice in that week.

          I would like to create a new variable "work" which reflects the fact that each of the worker complete the work in every 3 days ( represented by Round 1), then continue the same work for next three days (represented by Round 2). And after every 6 days, they take 1 day off. Before they continue with Round 3 work
          If I understand you correctly
          a) for each worker the work week is divided into two Rounds, Sunday-Tuesday and Wednesday-Saturday (although workers can take Saturday off if they have done their full week's work by then).
          b) the earliest work week for each worker defines that worker's "Round 1" and "Round 2"
          c) the variable you call "work" is to be the round number for that worker; I call it round in my code
          c) the table command I include to summarize the results uses Stata 17; since you didn't tell us otherwise we assume that you use the latest version
          d) I fixed your observation that spanned two lines - you clearly have some cleanup work to do to remove extra spaces and other incorrect characters from your NameoftheStudent variable.

          Perhaps this will start you towards the results I understand you to want.
          Code:
          // for each Date calculate the day of the week
          generate day = dow(Date)
          label define DOW 0 Sun 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri 6 Sat
          label values day DOW
          
          // for each dlp/Date calculate the week that observation is in for that dlp
          by dlp (Date), sort: generate week = floor((Date-Date[1]+day[1])/7) + 1
          
          // now calculate the round for that dlp, given the week and day
          generate round = 2 * week - cond(day<=2,1,0)
          
          // demonstrate results  
          table (week round) day, nototals
          Code:
          . table (week round) day, nototals
          
          ----------------------------------------------------
                    |                    day                  
                    |  Sun   Mon   Tue   Wed   Thu   Fri   Sat
          ----------+-----------------------------------------
          week      |                                         
            1       |                                         
              round |                                         
                2   |                            3     2      
            2       |                                         
              round |                                         
                3   |    3     3     2                        
                4   |                      1     2     2      
            3       |                                         
              round |                                         
                5   |    2     1     3                        
                6   |                            1     3      
            4       |                                         
              round |                                         
                7   |                5                        
                8   |                            3     1      
            5       |                                         
              round |                                         
                9   |    1     2     1                        
                10  |                      1     2           1
            6       |                                         
              round |                                         
                11  |          3                              
                12  |                      2     1            
          ----------------------------------------------------

          Comment


          • #6
            thank you so much William... the code works perfectly....

            Comment

            Working...
            X