Announcement

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

  • Storing output after "reshape"

    Hello,

    I need to store values after the reshape wide command, such that I can use them in the forvalues loop.

    That is, I have managed to extract the number of X_ij variables using the code below:

    Code:
    . char list _dta[ReS_Xij_n]
      _dta[ReS_Xij_n]:            3
    How do I then insert this value (3) in the forvalues loop?

    forvalues i=1(1)_dta[ReS_Xij_n] does not work and I could not find a solution so far. I tried doing it through scalar x=_dta[ReS_Xij_n], but this returns me _dta not found.

  • #2
    The number of variables you want is presumably the number of distinct values of whatever variable you feed to the j() option. So use saved results from levelsof or tabulate to get this number before the reshape.

    Comment


    • #3
      Marco Tacchi -

      Note that the output of help char tells us that
      Code:
      local num : char _dta[ReS_Xij_n]
      will place the value of the characteristic in the local macro num, and subsequently
      Code:
      forvalues i=1(1)`num'
      or
      Code:
      forvalues i=1/`num'
      will perform the loop you want.

      Comment


      • #4
        Originally posted by William Lisowski View Post
        Marco Tacchi -

        Note that the output of help char tells us that
        Code:
        local num : char _dta[ReS_Xij_n]
        will place the value of the characteristic in the local macro num, and subsequently
        Code:
        forvalues i=1(1)`num'
        or
        Code:
        forvalues i=1/`num'
        will perform the loop you want.
        For some reason the use of the local command from the help file did not work. I got the invalid syntax r(198); error.

        Nick's solution worked. I was also in general wondering if Stata allows capturing the last available output such as the result of the char list _dta[ReS_Xij_n] command.

        Thank you William and Nick.

        Comment


        • #5
          I am sorry, I did not realize fromthatNick's post was also telling you that _dat[ReS_Xij_n] is not the number that you want for your loop.

          The sample code below shows the values produced by Nick's technique and mine, and demonstrates that the local command does indeed work, so the reason you got the invalid syntax is presumably that your syntax was incorrect.

          Code:
          . levelsof type
          1 2
          
          . local nz = r(r)
          
          . reshape wide z, i(id) j(type)
          (note: j = 1 2)
          
          Data                               long   ->   wide
          -----------------------------------------------------------------------------
          Number of obs.                       10   ->      10
          Number of variables                   3   ->       3
          j variable (2 values)              type   ->   (dropped)
          xij variables:
                                                z   ->   z1 z2
          -----------------------------------------------------------------------------
          
          . char list
            _dta[ReS_Xij_n]:            1
            _dta[ReS_Xij_long1]:        z
            _dta[ReS_Xij_wide1]:        z1 z2
            _dta[ReS_i]:                id
            _dta[ReS_ver]:              v.2
            _dta[ReS_j]:                type
            _dta[ReS_str]:              0
            _dta[ReS_Xij]:              z
          
          . local num : char _dta[ReS_Xij_n]
          
          . display " nz = `nz' and num = `num' "
           nz = 2 and num = 1

          Comment

          Working...
          X