Announcement

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

  • Create a variable that contains a list of the observations of another variable

    Hello everyone,

    I have a question regarding how to make a new variable that lists all of the observations that fulfil a certain condition. In practice, I have one variable that contains a list of either a stock code, or a 0 if the condition is not fulfilled. Now, I need to make a summary variable, that in one cell lists all of the observations that contain numbers.

    For example, I could have the following:
    Observation number Stock Code
    1 0
    2 2345
    3 6789
    4 0
    5 1111


    So now, what I would want to do, is to create a new variable that equals: 2345 6789 1111, so basically the three strings one after the other with a space in-between. Does anyone have an idea of how to go about this?

    Thanks everyone for your consideration!

    Best regards,
    Janthe





  • #2
    I doubt that a new variable is the way to do this, but first check out levelsof.

    Comment


    • #3
      Thank you for your help, this is indeed what I am looking for!

      However, when I try to store the levelsof as: gen p = r(levels) stata gives me the error message "type mismatch".
      Any idea what might be causing this?

      Comment


      • #4
        doing things this way means that p will be a string variable so you need to insert "str" after "gen"

        it is not at all clear why you want to do this, however, so it is therefor also not clear if this is the best, or even a reasonable way, to get where you want

        Comment


        • #5
          If gen p = r(levels) gives the error "type mismatch," then Janthe De Nil is using an older version of Stata. With the current version 13, it is not necessary to specify -str- after -gen-.

          But, as others have pointed out, it is likely that the entire quest to create this variable is misguided. There is no point in creating a "variable" that doesn't vary! If the purpose is to capture a list of the code values for further use later, it is best to do this as a local macro:

          Code:
          levelsof stock_code
          local stock_code `r(levels)'
          The list of stock codes can then be referenced later as `stock_code', or manipulated with the many functions applicable to Stata macros.

          Two points: depending on how old a version of Stata is in use, -levelsof- may need to be just -levels-. Also, for any version of Stata before 13, strings are limited to 244 characters, so it is important not to use an equals sign in the command creating local stock_code. An equals sign would force evaluation of `r(levels)' as a string, and would truncate it to 244 characters. If there are really only three stock codes that isn't a problem, but in a more realistic scenario it would bite badly.

          Comment


          • #6
            Clyde gives excellent advice (I was out for a meal). One detail can be added:

            Code:
            levelsof stock_code, local(stock_code)
            gets you there in one, as levelsof has an option to create the local in one, precisely because this is one of the most needed results of this command.

            Comment


            • #7
              Thank you everyone! The probleem was indeed that I did not declare my variable as a string, and therefore the error message was mismatch.
              As for the point of this variable, I am actually using the code within a loop, so it makes more sense within the total loop.

              Thanks again for your help!

              Comment


              • #8
                If you are doing something similar to tasks described in http://www.stata.com/support/faqs/da...-with-foreach/ there are various good approaches, but none require a variable containing the same levels again and again in each observation.

                Comment

                Working...
                X