Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: generating automated filenames together with -svmat-


From   Michael Mulcahy <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: generating automated filenames together with -svmat-
Date   Fri, 8 Mar 2013 14:00:51 -0800 (PST)

Hi all,

I have 70 excel files containing annual measures. They are named 1919ae, 1920ae, 1921ae, etc. 


I am trying to write a program that loops through all the excel files in the folder, and:
1. imports them into stata
2. recodes some variables
3. uses -mkmat- and some matrix commands to create / manipulate matrices from the stata dataset
4. saves the resulting matrix using -svmat-, using part of the original file name in the name of the new file - I want the saved matrices to retain the "year" from the title of the original.

I'm using the code shown below (not very elegant, I know!). I have added notes in the code indicating where it seems to break down.
I have also tried to use the user-written-matsave- and -xsvmat- (references below), but the (I think) same problem arises. The automated file-naming approach is based on the UCLA: Statistical consulting group's page, "How Can I Generate Automated Filenames?"

Any guidance greatly appreciated! Thank you for your consideration! 

Mike


Do-File:
qui fs  *.xls
foreach f in `r(files)'  {  
    qui: import excel `f', firstrow clear
    local yr = substr("`f'", 1, 4)
    display "`yr'"
    renvars _all, lower
    renvars _all , prefix(ren)
    rename renid idd
    rename rengroup grr
            foreach var of varlist ren*  {
                    recode `var' 0 = -1
                    recode  `var' 3 =0
                    recode `var' 2 = 0
                    }
    dropmiss, force
    tostring idd, replace force
    tostring grr, replace force
    gen id = idd + grr
    destring id, replace
    drop idd  grr
    order id

save r`yr', replace /* NOTE TO STATALISTERS: it generates new files with the names "rYEAR", e.g. r1919, r1920, etc.*/
}


qui fs  *.dta
foreach f in `r(files)'  {  

local yr = substr("`f'", 2, 4)

mkmat ren*, matrix(X) rownames(id)
matrix XX = X * X'          /*NOTE TO STATALISTERS: Up to this point, everything works - the command matrix list XX displays what I                                              expect it to display */
svmat XX, names(col)     /*NOTE TO STATALISTERS: here, or in the next line, is where it seems to break down - I get a "syntax error" error code */
save s`f', replace
}

References: 
 
"How can I generate automated filenames in Stata?"
UCLA: Statistical Consulting Group.
http://www.ats.ucla.edu/stat/stata/faq/automated_filenames.htm (accessed March 7, 2013)
-matsave- user-written command by Marc-Andreas Muendler, Assistant Professor, Department of
Economics, University of California, San Diego.
-xsvmat-  user-written command by Roger Newson, National Heart and Lung Institute, Imperial College London, UK.  Email:
    [email protected]

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index