Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

Re: st: generating automated filenames together with -svmat-

From   Nick Cox <>
Subject   Re: st: generating automated filenames together with -svmat-
Date   Mon, 11 Mar 2013 13:02:12 +0000

The following user-written programs are invoked here:

fs (SSC)
renvars (SJ)
dropmiss (SJ)

Please remember to explain where user-written programs you use come from.

However, that is not central to Michael's problem.

What seems to fail is

svmat XX, names(col)

and so I suggest asthe next step looking at the column names of your
matrix -XX-,  as

"names(col) uses the column names of the matrix to name the variables"
 (-svmat- help)

and my guess is simply that the column names are not suitable.
(Perhaps they are just numbers.)


rename renid idd
rename rengroup grr
tostring idd, replace force
tostring grr, replace force
gen id = idd + grr
destring id, replace
drop idd  grr

could be I think condensed to

gen long id = real(string(renid) + string(rengroup))
drop renid rengroup

although I worry about your use of -force-.


On Fri, Mar 8, 2013 at 10:00 PM, Michael Mulcahy
<> wrote:

> 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.
> (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:

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index