Announcement

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

  • Help with creating a single variable which stores names of variables as observations

    I want to store the variables names in a single variable if its value is 1. For example, I would like to have a variable called sources which would have "source_home" if its value is 1 for the observation. It should only select the value with the first 1 and ignore if there are any others like in the last case here.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(source_home source_work source_food)
    0 0 0
    0 1 0
    0 0 1
    0 0 0
    0 0 0
    1 0 0
    0 0 0
    0 0 1
    0 0 0
    1 0 1
    end
    Thank you.

  • #2
    Presumably you have a time variable too: otherwise, there is no clear meaning to the idea of "first". Is there other structure such as your data being panel or longitudinal data?

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Presumably you have a time variable too: otherwise, there is no clear meaning to the idea of "first". Is there other structure such as your data being panel or longitudinal data?
      No, there is no time variable. It is longitudinal data. I want the variable names to be stored as observation in a new variable if its value is 1 and ignore the rest of variables from the varlist.

      Comment


      • #4
        #2
        there is no time variable. It is longitudinal data
        https://caldercenter.org/what-are-longitudinal-data


        A dataset is longitudinal if it tracks the same type of information on the same subjects at multiple points in time

        Comment


        • #5
          I apologise. It is simply a cross-sectional data with no time variable. I am trying to see the one source from each observation. It goes like, if source_home is 0 then it should select source_work and so on, but only if the previous source = 0. I hope this helps.

          Thank you.

          Comment


          • #6
            Thanks for the clarification, so if I understand correctly there is no point to identifying a first observation (in Stata meaning case, record or row). It seems that you may want something like


            Code:
            gen which = "" 
            
            foreach s in home work food { 
                  replace which = "`s'" if source_`s' == 1 
            }
            or


            Code:
            gen which = cond(source_home == 1, "home", cond(source_work == 1, "work", cond(source_food == 1, "food", "")))

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Thanks for the clarification, so if I understand correctly there is no point to identifying a first observation (in Stata meaning case, record or row). It seems that you may want something like


              Code:
              gen which = ""
              
              foreach s in home work food {
              replace which = "`s'" if source_`s' == 1
              }
              or


              Code:
              gen which = cond(source_home == 1, "home", cond(source_work == 1, "work", cond(source_food == 1, "food", "")))
              Thank you! Can you please also tell how I would be able to stop replacing the value in "which" if it has already taken used variable which has source == 1? Thank you.

              Comment


              • #8
                Code:
                 
                 replace which = "`s'" if source_`s' == 1 & which == ""
                Such code makes results a side effect of the order of work, home and food. You could also concatenate to get double and triple mentions.

                Comment

                Working...
                X