Announcement

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

  • Splitting Variables Into Groups

    I have a list of covariates " v* " all of which need to included in IRT models. In some of the data sets I am using v* contains over 2,000 variables, so I need to divide v* into smaller groups of ~100 variables per group. Given the amount of data I'm working with, "keep" and "drop" are not practical options if they involve naming specific variables in v* or even variable ranges with specific starting and ending variables. I tried the code below from http://www.stata.com/support/faqs/da...ts-of-dataset/ but got a "Group not found" r 111 error.

    preserve forval i = 1/7 { keep if group == `i' save group`i' restore, preserve }
    Thanks in advance for any help, and apologies in advance for bringing up a topic I know must have been covered many times here. I could not find the right search term to yield the thread that would help.

    Bryan

  • #2
    we can't tell what you actually typed (as shown above it is illegal); please read the FAQ on how to show code within CODE blocks and try again

    Comment


    • #3
      Apologies. Proper format should be below.

      I have a list of covariates v* all of which need to included in IRT models. In some of the data sets I am using v* contains over 2,000 variables, so I need to divide v* into smaller groups of ~100 variables per group. Given the amount of data I'm working with, "keep" and "drop" are not practical options if they involve naming specific variables in v* or even variable ranges with specific starting and ending variables. I tried the code below from http://www.stata.com/support/faqs/da...ts-of-dataset/ but got a "Group not found" r 111 error.

      Code:
        preserve 
        forval i = 1/7 {
                keep if group == `i'
                save group`i'
                restore, preserve 
        }

      Comment


      • #4
        it appears that there is no variable called "group" in your data - you need to set one up; you don't give enough information about your variable names for us to know whether something simple will do

        Comment


        • #5
          There may be a simpler way to do this, but the following should work:

          Code:
          local group_size 100
          
          unab varlist: _all
          
          tokenize `varlist'
          local counter 1
          while `"`1'"' != "" {
              local group
              forvalues i = 1/`group_size' {
                  local group `group' `1'
                  macro shift
              }
              preserve
              keep `group'
              save `roup`counter'
              des
              restore
              local ++counter
          }

          Comment


          • #6
            Variables names are "id" "gender" "race" and the variables of v*. The variables of v* are all named using the letter v and a number, so "v70" "v83" etc. The numbers after the v are neither consecutive nor spaced at regular intervals.

            Comment


            • #7
              Clyde, thank you. Stata returned the following:

              invalid 'roup1'
              r(198);

              Thoughts?

              Thanks,

              Bryan


              Originally posted by Clyde Schechter View Post
              There may be a simpler way to do this, but the following should work:

              Code:
              local group_size 100
              
              unab varlist: _all
              
              tokenize `varlist'
              local counter 1
              while `"`1'"' != "" {
              local group
              forvalues i = 1/`group_size' {
              local group `group' `1'
              macro shift
              }
              preserve
              keep `group'
              save `roup`counter'
              des
              restore
              local ++counter
              }

              Comment


              • #8
                Drat! There's a typo. The line that reads -save `roup`counter'- should read -save group`counter'- Sorry about that.

                Comment

                Working...
                X