Announcement

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

  • how to generate a new variable taking considering multiple variables

    Hi experts,

    I would like to ask how to generate a new variable taking considering multiple variables.

    For instance, let us assume there are three variables: id, year, and daily_income.

    id means people on dataset

    year 0 means the first year and year 1 means the second year.

    daily income is the amount of income they get daily.



    Click image for larger version

Name:	Screenshot 2022-08-20 122251.png
Views:	1
Size:	8.6 KB
ID:	1678460


    .

    What I want to do is to create a new variable called "poverty_category" to categorize people like the following;

    poverty_category = 1 if a person gets daily income more than 2 at both year 0 and year 1
    poverty_category = 2 if a person gets daily income less than 2 at year 1 but gets daily income more than 2 at year 2
    poverty_category = 3 if a person gets daily income more than 2 at year 1 but gets daily income less than 2 at year 2
    poverty_category = 4 if a person gets daily income less than 2 at both year 0 and year 1
    (For instance, if a person has 2 daily_income at year 1 and 2, he or she gets the value 1 for poverty_category )

    To summarize, I want to create the new variable like below, but I am not sure how to get there. So, could you please help me to realize what I want to do?
    Click image for larger version

Name:	Screenshot 2022-08-20 123011.png
Views:	1
Size:	13.9 KB
ID:	1678461


    Attached Files
    Last edited by Kenta Kunte; 19 Aug 2022, 22:30.

  • #2
    poverty_category = 1 if a person gets daily income less than 2 at both year 0 and year 1
    Did you mean poverty_category = 4?

    And what do you want to do when daily_income = 2? How does that play into your rules, which deal only with < 2 and > 2?

    Is the variable year always 0 or 1, and does every id have data for both years?

    Finally, please read the Forum FAQ, with particular attention to #12 which describes the most useful ways to present information in your posts. In particular, screenshots of data are not helpful: there is no way to import a screenshot into Stata to work with the data. Please, when posting back, and in all future posts, use the -dataex- command to show example data. If you are running version 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.

    Comment


    • #3
      Dear Clyde,

      Thank you so much for your comment. I correct misinformation and uploaded the excel file, not screenshot.
      Actually, I have just started using Stata yesterday, and this is my first to time to ask experts... Thank you so much

      Comment


      • #4
        I am assuming here that when you say "more than", you mean "at least", and that you made a mistake in constructing the poverty_category variable in your sample dataset (your id 2 should actually fall in category 3, and id 3 in 2, following your description).

        Code:
        import excel using test.xlsx, clear firstrow
        
        bys id (year): gen byte poverty =    cond(daily_income[1] >= 2 & daily_income[2] >= 2, 1, ///
                                    cond(daily_income[1] < 2 & daily_income[2] >= 2, 2, ///
                                    cond(daily_income[1] >= 2 & daily_income[2] < 2, 3, 4)))
        Last edited by Hemanshu Kumar; 20 Aug 2022, 00:11.

        Comment


        • #5
          Dear Hemanshu,

          Thank you for your comment. Thankfully, I could resolve the issue!

          Comment

          Working...
          X