Announcement

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

  • Creating table based on one observation

    I'm messing around with Stat's putpdf feature, trying to generate simple reports. I want to design a table in the PDF that in one column has a number of variable names (or descriptions would be better) and in the second column, the value of that variable for the specified observation. For example:

    For observation 16, the value for variable X is 2, the value for variable Y is 5.5, and the value for variable Z is 16.

    Table 1 - Observation 16
    Variable Value
    X 2
    Y 5.5
    Z 16

    Is there a good way to do this? I thought about transposing the data so the observations are the columns. I did this using sxpose because there are a number of string variables in my data set. When I transpose the data, I lose the variable names and it is difficult to keep track of them. I know there is an option to keep the variable names when using xpose, but that drops string variables in my experience and I cannot have that happen. It would be easiest for me to find a way to display the data without having to transpose it.

    Thanks in advance!

  • #2
    Perhaps this code example will do what you want, but there may be a more elegant way to do this. (This should work regardless of how many observations you retain in the data set.)
    Code:
    sysuse auto, clear
    
    * Reduce/simplify demo set to 4 cases with three variables.
    keep make price mpg
    keep if _n < 5
    
    ds
    local myvars `r(varlist)'
    
    sxpose, force clear
    
    gen _varname = ""
    
    local lineno = 0
    foreach v in `myvars' {
      local ++lineno
      replace _varname = "`v'" in `lineno'
      }
    
    order _varname
    
    list
    Red Owl
    Stata/IC 16.0 (Windows 10, 64-bit)
    Last edited by Red Owl; 10 Sep 2019, 11:47.

    Comment


    • #3
      Originally posted by Red Owl View Post
      Perhaps this code example will do what you want, but there may be a more elegant way to do this. (This should work regardless of how many observations you retain in the data set.)
      Code:
      sysuse auto, clear
      
      * Reduce/simplify demo set to 4 cases with three variables.
      keep make price mpg
      keep if _n < 5
      
      ds
      local myvars `r(varlist)'
      
      sxpose, force clear
      
      gen _varname = ""
      
      local lineno = 0
      foreach v in `myvars' {
      local ++lineno
      replace _varname = "`v'" in `lineno'
      }
      
      order _varname
      
      list
      Red Owl
      Stata/IC 16.0 (Windows 10, 64-bit)
      That worked incredibly well to transpose the data! Thanks for the help.

      Comment

      Working...
      X