Announcement

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

  • Is there a good way to store scalars in a dataset?

    Sometimes, we may need to record some scalars associated with a dataset, such as the value of a parameter we used to calculate some variables in the dataset. Is there a good way to store these scalars in the same dataset? Creating a variable to store a scalar (gen var1=scalar1) does not seem an efficient solution since it stores many copies of the same value. I thought of storing values in label definitions, but it sounds like a kludge. I hope there are good solutions. Thank you.

  • #2
    summ x1
    scalar B1 = r(mean)

    scalar B1 holds the mean of x1.

    unlike local, scalar sticks around. locals disappear, which can be useful.

    Comment


    • #3
      To explain george Ford's point differently, define scalars with the scalar command and then save the dataset. The scalars are included.

      Code:
      scalar list
      tells you what you gave.

      Comment


      • #4
        Thanks to George and Nick. I tried the suggested solution, but I might have done something wrong. I defined a scalar, saved the dataset, and exited Stata. When I reloaded the dataset, I did not see the scalar I defined in the previous session. I was hoping that the scalar would become a permanent part of the dataset. Thank you.

        Comment


        • #5
          It should be. Something is amiss.

          scalar list before you save to make sure they are there.

          make sure you replace when you save (or rename it to retain the original).



          Comment


          • #6
            I can't replicate the notion that scalars are preserved in Stata datasets across sessions (or after -clear all-).

            Code:
            clear *
            cls
            set seed 17
            
            set obs 10
            gen y = rnormal()
            
            summ y, meanonly
            scalar mean = r(mean)
            scalar list
            
            save test, replace
            
            clear * // clear all results, or simply start a new Stata session
            
            use test, clear
            scalar list
            As a workaround, you can create a frame and post your scalars into that frame, then save the resulting dataset. You will not have those values loaded as scalars again in a new instance of Stata, but you have the names and values, and you could write a simple enough loop or whatever to load them back up. Still a bit clunky, but less so than the proposed idea in #1.

            Code:
            frame create myscalars str32(name) double(val)
            frame post myscalars ("mean") (r(mean))
            frame myscalars: list

            Comment


            • #7
              I concur, after clear all.

              Comment


              • #8
                The magic is in the arcane char command, but with the warning that char is designed to store strings, not numbers, so if you want full precision you have to format it with an equally arcane format.
                Code:
                clear *
                cls
                set seed 17
                
                set obs 10
                gen y = rnormal()
                
                summ y, meanonly
                scalar mean = r(mean)
                scalar list
                
                local cmean = strofreal(mean,"%21x")
                macro list _cmean
                char _dta[mean] `cmean'
                char list
                
                save test, replace
                
                clear * // clear all results, or simply start a new Stata session
                
                use test, clear
                scalar list
                char list
                
                local cmean : char _dta[mean]
                macro list _cmean
                scalar mean = `cmean'
                scalar list
                Code:
                . set seed 17
                
                . 
                . set obs 10
                Number of observations (_N) was 0, now 10.
                
                . gen y = rnormal()
                
                . 
                . summ y, meanonly
                
                . scalar mean = r(mean)
                
                . scalar list
                      mean =  .12692929
                
                . 
                . local cmean = strofreal(mean,"%21x")
                
                . macro list _cmean
                _cmean:         +1.03f3816666666X-003
                
                . char _dta[mean] `cmean'
                
                . char list
                  _dta[mean]:                 +1.03f3816666666X-003
                
                . 
                . save test, replace
                file test.dta saved
                
                . 
                . clear * // clear all results, or simply start a new Stata session
                
                . 
                . use test, clear
                
                . scalar list
                
                . char list
                  _dta[mean]:                 +1.03f3816666666X-003
                
                . 
                . local cmean : char _dta[mean]
                
                . macro list _cmean
                _cmean:         +1.03f3816666666X-003
                
                . scalar mean = `cmean'
                
                . scalar list
                      mean =  .12692929
                
                .

                Comment


                • #9
                  Sorry, #3 from me was WRONG. Wishful thinkjng strikes again....

                  Comment


                  • #10
                    ]I suppose you could stack the scalars in a matrix, svmat and save, and then recall to scalars when you run the do file again.

                    Code:
                    use auto, clear
                    local counter = 1
                    foreach var in price mpg headroom weight length {
                        summ `var'
                        scalar M`counter' = r(mean)
                        local counter = `counter'+1
                    }
                    scalar list
                    g SCALARS = .
                    forv i = 1/5 {
                        replace SCALARS = M`i' in `i'
                    }
                    save auto1, replace
                    clear all
                    use auto1
                    forv i = 1/5 {
                        scalar M`i' = SCALARS[`i']
                    }
                    scalar list

                    Comment

                    Working...
                    X