Announcement

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

  • Get temporary variables from other frame

    Hi all,

    A programme that I have been attempting to write relies on some calculations in a temporary data frame, whose results are stored in temporary variables in that data frame. I want to get these results back into the main data frame, but Stata simply will not copy the variables from the temporary frame into the main frame. I suspect that this is due to some built-in restrictions on temporary variables because the problem does not arise when I use normal variables. However, that is not an option in my setting, since my programme has to work in very generic settings, sometimes with hundreds of variables, and I cannot guarantee that a given variable name is not taken already.

    As a simple working example, consider the following code:

    Code:
    sysuse citytemp
    
    tempname newframe
    frame put division heatdd, into(`newframe')
    frame change `newframe'
        tempvar sum_heatdd
        collapse (sum) `sum_heatdd' = heatdd, by(division)
    frame change default
    
    frlink m:1 division, frame(`newframe')
    frget `sum_heatdd', from(`newframe')
    
    gen sum_heatdd = `sum_heatdd'
    This results in an error, because `sum_heatdd' is not copied into the main data frame. Is there any way to work around this restriction? (I also do not want to use a preserve/save/restore/merge workflow because I need to use the programme quite frequently and disk storage is painstakingly slow on the machine I have to work on.)

    Any help would be greatly appreciated!

    Cheers,

    Joscha

  • #2
    Well, the following workaround will get you what you want:
    Code:
    sysuse citytemp, clear
    
    tempname newframe
    frame put division heatdd, into(`newframe')
    frame change `newframe'
        tempvar sum_heatdd
        collapse (sum) `sum_heatdd' = heatdd, by(division)
    frame change default
    
    frlink m:1 division, frame(`newframe')
    
    gen sum_heatdd = frval(`newframe', `sum_heatdd')
    Well, almost. It will create the variable sum_heatdd with the correct values. But it does not clone it the way -frget- does,so in the (unlikely) event that `sum_heatdd' is value or varlabeled, or has other than default storage type or display format, you will need to attend to those matters separately.

    I 'm not going to call this a bug in Stata. The PDF documentation does say that variables beginning with _ are not copied, because those are mostly Stata system variables that "you do not want." One could disagree about their judgment that you would not want such variables. So it is arguably a design error, although I do see their point and would be more likely to call this a debatable design decision, one that is rescued by the availability of the -frval()- workaround.

    Comment


    • #3
      frget will not copy variables with names that start with underscore _. You can override this behavior by providing a new variable name for frget to copy into the current frame. You can even reuse the original name, provided it is not already a variable in the current frame.
      Code:
      frget `sum_heatdd'=`sum_heatdd', from(`newframe')
      This behavior is documented in [D] frget, under 4. of section "Everything you need to know about frget", click here to see this in your browser.

      Comment


      • #4
        Thanks so much for your help - something like the -frval()- function was exactly what I was looking for!

        Comment

        Working...
        X