r - How to find a common variable in a large number of databases using Stata -


so have large number of databases (82) in stata, each contain around 1300 variables , several thousand observations. of these databases contain variables give mean or standard deviation of concepts. example, variable in such dataset called "leverage_mean". now, want know datasets contain variables called concept_mean or concept_sd, without having go through every dataset hand.

i thinking maybe there way loop through databases looking variables containing "mean" or "sd", unfortunately have idea how this. i'm using r , stata datafiles.

yes, can loop in stata r. first, should check out stata command ds , package findname, many of things described here , more. show happening "under hood", i'll show stata code can achieve below:

/*set current directory location of databases*/ cd "[your cd here]" 

save names of 82 databases list called "filelist" using stata's dir function macros. note: don't specify kind of file database files are, i'm assuming .xls. command saves files extension ".xls" list. type of file save list , how import database depend on type of files reading in.

local filelist : dir . files "*.xls" 

then loop on files show ones contain variables end "_sd" or "_mean".

foreach file of local filelist {     /*import data*/     import excel "`file'", firstrow clear case(lower)      /*produce list of variables end "_sd" , "_mean"*/      cap quietly describe *_sd *_mean, varlist     if length("r(varlist)") > 0 {      /*if database contains variables of interest, display database file name , variables on screen*/          display "database `file' contains variables: " r(varlist)     } } 

final note, loop display database name , variables of interest contained within it. if want perform actions on data, or else, actions need included in position of final "display" command (which may or may not need).


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -