Announcement

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

  • Creating variables that sum variables in regularly interspaced columns

    Hi, I would like to create a formula for making variables of the kind:

    gen newvar = v[k] + v[k+8] + v[k+16] + v[k+24] + v[k+32]

    where I will specify the k.

    Any idea how I can execute this?

    Thanks

  • #2
    That's potentially legal syntax if (and only if) v is a single variable, but your phrasing in terms of columns leads me to imagine that you are combining results from different variables.

    In Stata columns are typically called variables.

    I think we need to see a data example to be clear on your data structure (layout). Indeed, from other posts I think it quite likely that you need a different layout after which most things will be easier than at present.

    Comment


    • #3
      Thank you for your response! The columns in my data are all different variables, which I would like to add at regular intervals. Rows are area codes. The columns are taken from an original excel sheet which had many columns within columns, which is why some columns are relevant to other columns at an interval of 8.

      I feel like this operation is something I should be able to achieve without re-formatting, what do you think?
      Click image for larger version

Name:	Screen Shot 2019-05-29 at 1.36.41 pm.png
Views:	3
Size:	24.6 KB
ID:	1500669


      Attached Files

      Comment


      • #4
        To illustrate further, these are the commands I would like to automate. I have to do a lot of them:
        Attached Files

        Comment


        • #5
          Please see https://www.statalist.org/forums/help#stata 12.2 for what we mean by data example (it's not a screenshot).

          You could avoid a reshape with systematic renaming. Meanwhile please show us the results of

          Code:
          ds
          and not as an attached image but as text results copied and pasted between code delimiters like this

          Code:
          stuff between code delimiters
          Again, the FAQ link just given explains.
          Last edited by Nick Cox; 29 May 2019, 05:02.

          Comment


          • #6
            How you approach this depends somewhat on how many other things you intend to do with this data.

            In general variable names like v2 v3 v4, etc. are terrible variable names because they don't tell you anything about the data contained within and are tremendously difficult to keep straight when writing (and reading) code. So if you're going to do any other tasks with these variables I would highly recommend some renaming. You could take advantage of the patterned structure of the columns to do that renaming, but since that is not the task for which you requested assistance I'll leave that aside for now.

            Are these 8 generate statements the full set of what you want to create? If so, you already have code that will work and there may not much to be gained in terms of readability to putting it into a loop. Just as an example you could do something like this.

            Code:
            local suffixes="ep eg eb ed ec eid ens ena"
            
            local c=2
            forv i=1/8 {
            
            local name : word `i' of `suffixes'
            
            gen M_ft_`name'=v`c'
                forv s=8(8)72 {
                    replace M_ft_`name'=M_ft_`name'+v`s'
                }
                
            local ++c
            
            }
            There are surely other strategies you could pursue as well. However, notice that this is harder to read than your original code. In fact if you're not a frequent user of loops this version is probably very difficult to understand. If this is a much larger task than what you've presented it might be worthwhile to pursue a looping strategy. However if you've already achieved the task you set out to accomplish with the code in your screenshot there may not be much to be gained by changing it.

            Back to the issue of renaming for a moment, though, I would note that you could fairly easily use a similar looping strategy to generate some more informative variable names.

            Comment

            Working...
            X