Announcement

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

  • "Shifting" down values while generating variable(s)

    Dear friends,
    I need to do the following: I have an original variable v1 and have to generate new variables v2-v10 "shifting" down values from the original v1 like follows:

    day v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
    1 1.33E-07 . . . . . . . . .
    2 1.25E-07 1.33E-07 . . . . . . . .
    3 1.20E-07 1.25E-07 1.33E-07 . . . . . . .
    4 1.15E-07 1.20E-07 1.25E-07 1.33E-07 . . . . . .
    5 1.12E-07 1.15E-07 1.20E-07 1.25E-07 1.33E-07 . . . . .
    6 1.10E-07 1.12E-07 1.15E-07 1.20E-07 1.25E-07 1.33E-07 . . . .
    7 1.08E-07 1.10E-07 1.12E-07 1.15E-07 1.20E-07 1.25E-07 1.33E-07 . . .
    8 1.07E-07 1.08E-07 1.10E-07 1.12E-07 1.15E-07 1.20E-07 1.25E-07 1.33E-07 . .
    9 1.05E-07 1.07E-07 1.08E-07 1.10E-07 1.12E-07 1.15E-07 1.20E-07 1.25E-07 1.33E-07 .
    10 1.04E-07 1.05E-07 1.07E-07 1.08E-07 1.10E-07 1.12E-07 1.15E-07 1.20E-07 1.25E-07 1.33E-07
    Is there any way to do this in Stata?

    Thank you very much.

    Mik

  • #2
    Code:
    tsset day
    forval t=2/10{
        gen v`t'= L`=`t'-1'.v1
    }
    Last edited by Andrew Musau; 23 Feb 2025, 06:57.

    Comment


    • #3
      With such small numbers you probably want to store them as doubles rather than floats. In that case replace gen v`t' with gen double v`t'
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        I'd add two details to Andrew Musau's fine answer.

        First, use informative variable names. If v1 is the variable name for your data (surely there is a better name, and indeed perhaps you are using it really) then something more like.

        Code:
        forval lag = 1/9 { 
              gen l`lag'_v1 = L`lag'.v1 
        }
        would mean that the names and the definitions do match. Otherwise the risk of becoming confused is too high.

        Second, if numbers are routinely about 1 in 10 million, you might be able to change units without real loss of information.

        Comment


        • #5
          Thank you very much, Andrew, this worked perfectly! I have to make myself familiar with those groups of commands in Stata...
          Sincerely,
          Mik

          Comment


          • #6
            Dear Nik, thank you very much for your note: off course I will follow your recommendation. More than that I have to make myself more familiar with Stata commands I do not use. Regarding that, by the way, I would suggest someone must write a book of "recipes" in Stata, if this can be done at all, something like Michael N. Mitchell - A Visual Guide to Stata Graphics
            Sincerely,
            Mik

            Comment


            • #7
              I am not clear especially what kind of book you seek, but there are many books on Stata from Stata Press and other publishers.

              As a reader and an author too, my experience is that authors write the books they want to write. Ultimately you may have to write the book you want to read.

              Further, books that stay close to the Stata manuals and books that go beyond them have different values, a range that isn't closely related to whether people are new to Stata or much more experienced.
              Last edited by Nick Cox; 23 Feb 2025, 08:38.

              Comment


              • #8
                Dear Nick,
                I apologize for "must write" - it might seem too strong indeed.
                You are right, there are many books on Stata covering lots of aspect of using this great software.
                And you are right about the fact that authors write the books they want to write - after all they submit these books to a free market where almost each book will (with a certain probability) find its reader.
                In an ideal world however authors would write books readers would like to read - but if only authors would know and where is that ideal world anyway!
                Thank you very much for your note again, it is very helpful and educational.\
                Sincerely,
                Mik

                Comment

                Working...
                X