Announcement

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

  • data management

    hi, how do I subset variable based on the observations within it ,For example (having malaria and not having malaria).Please help.I have really struggled

  • #2
    Please also help me generate a new variable from another variable (which has numeric responses )where the first digit from each response is to form the raw result for the newly generated variable.

    Comment


    • #3
      Hi Owino,

      It would be helpful if you posted a sample of your data using -dataex- or using the table tools (dataex is the preferred way when you post this to the main Statalist forum). The below may not do exactly what you want, but hopefully can point you in the right direction.

      For question #1, if you want to create a variable based on another variable (say diseases_person_had) that contains the text "malaria":

      Code:
      gen has_malaria = 1 if strpos(diseases_person_had, strupper("malaria")) > 0
      * NOTE: strpos is case sensitive, so "Malaria" is different from "malaria"
      For question #2, to generate a new variable based on the first-digit of a numeric variable, you could do it a couple of ways, but I would be tempted to convert the numeric value to a string, and then use substr()
      Code:
      * Assume the numeric variable with the response is response_var
      tostring response_var, gen(response_var_string)
      gen first_digit = substr(response_var_string, 1, 1)
      destring first_digit, replace
      Last edited by David Benson; 30 Oct 2018, 15:49.

      Comment


      • #4
        Welcome to Statalist! Unfortunately, you're in the wrong forum. Use this forum to make test posts, practice with the formatting features of the forum software, etc. Post questions about Stata in the General Forum. Before you do, be sure to read the FAQ,especially FAQ 12 about how to ask a good question. In particular post example using the dataex command and present code and results between [CODE] and [/CODE] delimiters.
        Steve Samuels
        Statistical Consulting
        [email protected]

        Stata 14.2

        Comment

        Working...
        X