Announcement

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

  • Create new variable based on observations

    Hi everyone,

    As I am new to using stata I have come up with a Problem which I do not find a solution for. Hopefully someone of you can help me with it.

    I have the following list

    date isin
    12/12/2013 XC0009699965
    12/12/2013 NL0009699965
    03/04/2016 XC0009667890
    03/11/2016 US0009619067
    03/29/2016 NL0009699965
    06/08/2016 XC0009699965

    For each isin I want to create a variable with the respective names "get_XC0009699965" "get_NL0009699965" "get_XC0009667890" and so on. My actual list contains about 2000 different isins which makes the programming even harder for me.

    Is there a way that stata runs through all isins and creates a new variable for each isin (only one variable per isin - no duplicates)?

    Thank you very much!

    Nina

  • #2
    I'm not really sure why you want this, but the below code works

    Code:
    clear
    input float date str12 isin
    19704 "XC0009699965"
    19704 "NL0009699965"
    20517 "XC0009667890"
    20524 "US0009619067"
    20542 "NL0009699965"
    20613 "XC0009699965"
    end
    format %td date
    
    levelsof isin, local(levels)
    foreach level of local levels{
        gen get_`level' =
    }
    Perhaps you mean that you want an observation (not a variable) for each value of isin. In which case, this is what you want

    Code:
    gen get_prefix = "get_" + isin

    Comment


    • #3
      I don't understand your question. It's easy enough to create new variables having those names. But what values are supposed to be put in those new variables?

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float date str12 isin
      19704 "XC0009699965"
      19704 "NL0009699965"
      20517 "XC0009667890"
      20524 "US0009619067"
      20542 "NL0009699965"
      20613 "XC0009699965"
      end
      format %td date
      
      
      levelsof isin, local(isins)
      foreach i of local isins {
          gen get_`i' = ???
      }
      Please read the FAQ, with particular attention to number 12. In the future, please post all example data using the -dataex- command, as I have done in this response. Your listing of the data is inconvenient to import into Stata and it is silent on whether your date variable is a bona fide Stata date variable or just a string readable by humans as a date. That isn't critical for your question, but if it had been, no possible answer would be forthcoming. When you use -dataex- you help those want to help you by enabling them to create a complete and faithful replica of your Stata example with a simple copy/paste operation.

      Added: Crossed with #2.

      Comment


      • #4
        Welcome to Statalist, Nina.

        To Chris and Clyde's responses let me add that it seems likely that adding 2000 new variables to each observation - which seems to be what you describe - will lead to a fair amount of complication and trouble in all of your subsequent analyses using that data.

        Since you are new to Stata, I worry that in seeking to reach your analytical goal, you have not found the best path, and have instead set off on a different path, then asked Statalist for help taking a step on this more difficult path.

        You should consider providing further explanation of what you are trying to accomplish that leads you to want to create those 2000 new variables. Perhaps Statalist can advise a path that will make it easier to accomplish what you seek.

        Comment

        Working...
        X