loup icon
user icon
Welcome to Onecklace
shopping bag
0
Last Chance for Christmas Delivery!
15% Off Everything | 25% Off 2+ Items

Gtools Macro Apr 2026

* Verify variable exists confirm variable `oldvar'

* Check if variable exists if "`replace'" == "" capture confirm new variable `newvar' if _rc != 0 di as error "Variable `newvar' already exists. Use replace option." exit 198

* Temporary variable for calculations tempvar tempvar * Apply if/in conditions marksample touse, novarlist mark `touse' `if' `in' * Create initial copy quietly gen `tempvar' = `oldvar' if `touse' gtools macro

*! gtools_macro.ado *! Full-featured data transformation and recoding tool *! Version 1.0

program define gtools_macro version 15 syntax [varlist] [if] [in] [, /// RECode(numlist) /// GENerate(string) /// LABel(string) /// CUTpoints(numlist) /// BINs(integer 5) /// STDize /// LOG /// SQRT /// SQUare /// RANK /// Center /// PErcentile(integer 10) /// TOPcode(real 99) /// BOTtomcode(real 1) /// MISSing(real .) /// REPLace /// DROPold /// VERBose /// SAVe(string) /// APPend(string) /// STATistics /// HELP /// ] * Verify variable exists confirm variable `oldvar' *

* Apply transformations in sequence local transform_count = 0 * 1. Topcode if "`topcode'" != "" quietly replace `tempvar' = `topcode' if `tempvar' > `topcode' & `tempvar' < . if "`verbose'" != "" di as text "Topcoded at: `topcode'" local transform_count = `transform_count' + 1 * 2. Bottomcode if "`bottomcode'" != "" quietly replace `tempvar' = `bottomcode' if `tempvar' < `bottomcode' & `tempvar' < . if "`verbose'" != "" di as text "Bottomcoded at: `bottomcode'" local transform_count = `transform_count' + 1 * 3. Missing values if "`missing'" != "" & `missing' != . quietly replace `tempvar' = . if `tempvar' == `missing' if "`verbose'" != "" di as text "Set value `missing' to missing" local transform_count = `transform_count' + 1 * 4. Recode if "`recode'" != "" local recode_list = "`recode'" * Parse recode rules (e.g., 1/5=1 6/10=2) local i = 1 while "`recode_list'" != "" gettoken rule recode_list: recode_list local equal_pos = strpos("`rule'", "=") if `equal_pos' > 0 local from = substr("`rule'", 1, `equal_pos'-1) local to = substr("`rule'", `equal_pos'+1, .) * Handle range (e.g., 1/5) local slash_pos = strpos("`from'", "/") if `slash_pos' > 0 local from_low = real(substr("`from'", 1, `slash_pos'-1)) local from_high = real(substr("`from'", `slash_pos'+1, .)) quietly replace `tempvar' = `to' if `tempvar' >= `from_low' & `tempvar' <= `from_high' & `tempvar' < . else quietly replace `tempvar' = `to' if `tempvar' == real("`from'") local i = `i' + 1 if "`verbose'" != "" di as text "Applied recoding" local transform_count = `transform_count' + 1 * 5. Cutpoints/Binning if "`cutpoints'" != "" local n_cuts: word count `cutpoints' local n_bins = `n_cuts' - 1 forvalues i = 1/`n_bins' local low: word `i' of `cutpoints' local high: word `=`i'+1' of `cutpoints' quietly replace `tempvar' = `i' if `tempvar' >= `low' & `tempvar' < `high' & `tempvar' < . if "`verbose'" != "" di as text "Binned using custom cutpoints" local transform_count = `transform_count' + 1 * 6. Equal-width bins if "`bins'" != "" & "`cutpoints'" == "" quietly summarize `tempvar' if `touse', meanonly local min = r(min) local max = r(max) local width = (`max' - `min') / `bins' forvalues i = 1/`bins' local low = `min' + (`i'-1)*`width' local high = `min' + `i'*`width' if `i' == `bins' quietly replace `tempvar' = `i' if `tempvar' >= `low' & `tempvar' <= `high' & `tempvar' < . else quietly replace `tempvar' = `i' if `tempvar' >= `low' & `tempvar' < `high' & `tempvar' < . if "`verbose'" != "" di as text "Created `bins' equal-width bins" local transform_count = `transform_count' + 1 * 7. Percentile groups if "`percentile'" != "" quietly egen `tempvar'_pct = pctile(`tempvar') if `touse', p(`percentile') quietly replace `tempvar' = `tempvar'_pct drop `tempvar'_pct if "`verbose'" != "" di as text "Created `percentile' percentile groups" local transform_count = `transform_count' + 1 * 8. Standardize if "`stdize'" != "" quietly summarize `tempvar' if `touse', meanonly local mean = r(mean) local sd = r(sd) quietly replace `tempvar' = (`tempvar' - `mean') / `sd' if "`verbose'" != "" di as text "Standardized (mean=`mean', sd=`sd')" local transform_count = `transform_count' + 1 * 9. Center if "`center'" != "" quietly summarize `tempvar' if `touse', meanonly local mean = r(mean) quietly replace `tempvar' = `tempvar' - `mean' if "`verbose'" != "" di as text "Centered (mean=`mean')" local transform_count = `transform_count' + 1 * 10. Log transformation if "`log'" != "" quietly replace `tempvar' = ln(`tempvar') if `tempvar' > 0 & `tempvar' < . if "`verbose'" != "" di as text "Applied natural log transformation" local transform_count = `transform_count' + 1 * 11. Square root if "`sqrt'" != "" quietly replace `tempvar' = sqrt(`tempvar') if `tempvar' >= 0 & `tempvar' < . if "`verbose'" != "" di as text "Applied square root transformation" local transform_count = `transform_count' + 1 * 12. Square if "`square'" != "" quietly replace `tempvar' = `tempvar'^2 if "`verbose'" != "" di as text "Applied square transformation" local transform_count = `transform_count' + 1 * 13. Rank if "`rank'" != "" quietly egen `tempvar'_rank = rank(`tempvar') if `touse' quietly replace `tempvar' = `tempvar'_rank drop `tempvar'_rank if "`verbose'" != "" di as text "Applied rank transformation" local transform_count = `transform_count' + 1 * Create final variable if "`replace'" != "" quietly replace `newvar' = `tempvar' if `touse' else quietly gen `newvar' = `tempvar' if `touse' * Apply variable label if "`label'" != "" label variable `newvar' "`label'" if "`verbose'" != "" di as text "Applied label: `label'" else if "`generate'" != "" label variable `newvar' "Transformed from `oldvar'" * Drop old variable if requested if "`dropold'" != "" & "`replace'" == "" drop `oldvar' if "`verbose'" != "" di as text "Dropped original variable: `oldvar'" * Display statistics if "`statistics'" != "" | "`verbose'" != "" di as text _n "Summary statistics for `newvar':" quietly summarize `newvar' if `touse', detail di as text " Observations: " as result r(N) di as text " Mean: " as result %9.4f r(mean) di as text " Std. Dev.: " as result %9.4f r(sd) di as text " Min: " as result %9.4f r(min) di as text " Max: " as result %9.4f r(max) di as text " Transformations applied: " as result `transform_count' * Save/Append results if "`save'" != "" preserve quietly tempfile results clear set obs 1 gen var = "`oldvar'" gen newvar = "`newvar'" gen N = r(N) gen mean = r(mean) gen sd = r(sd) gen min = r(min) gen max = r(max) gen transforms = `transform_count' gen date = date(c(current_date), "DMY") format date %td save "`save'", replace restore if "`verbose'" != "" di as text "Results saved to: `save'" if "`append'" != "" preserve quietly tempfile results clear set obs 1 gen var = "`oldvar'" gen newvar = "`newvar'" gen N = r(N) gen mean = r(mean) gen sd = r(sd) gen min = r(min) gen max = r(max) gen transforms = `transform_count' gen date = date(c(current_date), "DMY") format date %td save "`results'" use "`append'", clear append using "`results'" save "`append'", replace restore if "`verbose'" != "" di as text "Results appended to: `append'" if "`verbose'" != "" di as text _n "gtools_macro completed successfully" end

* Parse variable list - use first variable only tokenize `varlist' local oldvar = "`1'" Full-featured data transformation and recoding tool *

* Help display if "`help'" != "" di as text _n "gtools_macro - Comprehensive data transformation tool" di as text "==================================================" di as text "Options:" di as text " recode(numlist) - Recode values (e.g., 1/5=1 6/10=2)" di as text " generate(string) - Name of new variable" di as text " label(string) - Variable label for new variable" di as text " cutpoints(numlist)- Cut points for binning" di as text " bins(#) - Number of equal-width bins (default: 5)" di as text " stdize - Standardize to mean 0, sd 1" di as text " log - Natural log transformation" di as text " sqrt - Square root transformation" di as text " square - Square transformation" di as text " rank - Rank transformation" di as text " center - Center by subtracting mean" di as text " percentile(#) - Create percentile groups" di as text " topcode(#) - Topcode values above #" di as text " bottomcode(#) - Bottomcode values below #" di as text " missing(#) - Set specific value to missing" di as text " replace - Replace existing variable" di as text " dropold - Drop original variable" di as text " verbose - Display detailed output" di as text " save(filename) - Save results to file" di as text " append(filename) - Append results to file" di as text " statistics - Show summary statistics" exit

* Determine new variable name if "`generate'" == "" & "`replace'" == "" local newvar = "`oldvar'_transformed" else if "`generate'" != "" local newvar = "`generate'" else if "`replace'" != "" local newvar = "`oldvar'"

* Check if varlist provided if "`varlist'" == "" di as error "Error: varlist required" exit 198

Why Onecklace

Handcrafted with Care

Every piece is made to order by our in-house team. No mass production - just thoughtful, detailed craftsmanship.

Personalized for Your Story

Our custom name necklaces and personalized jewelry let you express who you are and celebrate the moments that matter.

Quality Materials

We use sterling silver, stainless steel, 18k gold plating, rose gold plating, and 14k solid gold for long-lasting shine and durability.

Trusted Worldwide

With over a decade of experience and thousands of real reviews, our customers know they can rely on us for quality and service.

Footer CC icons
© 2024 Onecklace.com All rights reserved
whatsapp Icon