Announcement

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

  • How can I create new variables from one or more parts of a large dataset?

    Dear all,

    Newbie question here.

    Usually, I split a large variable creating new dataset for each value of this variable:

    Code:
     preserve
     foreach i of num 1/8 {
             keep if BigVar == `i'
             save BigVar`i'
             restore, preserve
     }
    This split BigVar in various separate datasets: BigVar1, BigVar2, etc...

    Now, instead of generating a brand new dataset, I would like only to create new variables. How can I do it?

    I tried

    Code:
     foreach i of num 1/8 {
             if BigVar == `i'
             gen BigVar`i'
     }
    But it doesn't work. I clearly don't know something very basic about STATA syntax.
    Can somebody help me with that?
    Last edited by Martinо Cоmelli; 07 Feb 2019, 08:45.

  • #2
    Stata works with only ONE dataset in memory, hence all variables have to be of the same length. You can cut the dataset with the keep command either horizontally with keep in 1/k or vertically with keep var1 var2 var3.

    In all likelihood you don't need to split anything in Stata. Instead look at the by prefix which allows you to do analysis by some groups, e.g defined by categories of a categorical variable,

    Best, Sergiy Radyakin

    Comment


    • #3
      Just to rephrase: I need to create new variables from a big variable. It's OK that the new variables have a lot of missing data, of course.

      Comment


      • #4
        Start with 12.2 here.

        Comment

        Working...
        X