-
Notifications
You must be signed in to change notification settings - Fork 1
/
strcompress.ado
54 lines (40 loc) · 1.38 KB
/
strcompress.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
strcompress: Stata module for maximally compressing string variables
Author: Luke Stein
Contact: lcdstein@babson.edu
Date: 2019-01-28
Version: 0.2
*/
capture program drop strcompress
program define strcompress
version 13.0
syntax [varlist]
qui memory
local oldmem = `r(data_data_u)' + `r(data_strl_u)'
qui ds `varlist', has(type str#)
if "`r(varlist)'" != "" {
disp as text " Converting " as result "str" as text " to " as result "strL" as text ": `r(varlist)'"
qui recast strL `r(varlist)'
qui memory
local midmem = `r(data_data_u)' + `r(data_strl_u)'
if `midmem' <= `oldmem' {
local savingsfrac = 1-(`midmem' / `oldmem')
disp as text " strcompress shrank data " as result %1.0f = 100*`savingsfrac' "%" as text " so far"
}
else {
local savingsfrac = (`midmem' / `oldmem')-1
disp as text " strcompress grew data " as result %1.0f = 100*`savingsfrac' "%" as text " so far, but I'm not done yet!"
}
}
else {
qui memory
local midmem = `r(data_data_u)' + `r(data_strl_u)'
}
compress `varlist'
qui memory
local newmem = `r(data_data_u)' + `r(data_strl_u)'
local savingsfrac = 1-(`newmem' / `midmem')
disp as text " compress shrank data " as result %2.0f = 100*`savingsfrac' "%"
local savingsfrac = 1-(`newmem' / `oldmem')
disp as text " strcompress shrank data " as result %2.0f = 100*`savingsfrac' "%" as text " overall"
end