Announcement

I would like to get the list of CSV files in the directory but, as always, I messed up something with quotes.

* Path and files global importfolder /// `"`C:\Users\me\files\data to play with'"' global filelist : dir $ *.csv
Kind regards,
Konrad
Version: Stata/IC 13.1 Tags: None Nick Cox 28 Aug 2014, 10:28
global importfolder "C:\Users\me\files\data to play with" global filelist : dir "$importfolder" *.csv

You just need plain " " here to bind over the spaces. The single quotes suggest some sort of local macro but you don't have one, so they are mistaken here.

EDIT: Copied a point from Maarten Buis.

Last edited by Nick Cox; 28 Aug 2014, 10:43 .

Comment

Post Cancel Maarten Buis 28 Aug 2014, 10:35

I suspect you want:

global importfolder /// `"C:\Users\me\files\data to play with"' global filelist : dir `"$"' files *.csv

i.e. no inner single quotes, as that would assume your path is actually a local macro and added quotes around the global macro again, as quotes get stripped off.

Comment

Post Cancel Konrad Zdeb 28 Aug 2014, 12:39

Thanks for all the comments. Following the "help quotes" section, I was trying to be fancy and encapsulate "" in the macro so I could call the path without typing "" around macro.

Kind regards,
Konrad
Version: Stata/IC 13.1

Comment

Post Cancel Konrad Zdeb 29 Aug 2014, 02:11
. global importfolder /// `"C:\Users\me\files\data to play with"' . global filelist : dir `"$"' files *.csv varlist not allowed
Last edited by Konrad Zdeb; 29 Aug 2014, 02:11 . Reason: Typo Kind regards,
Konrad
Version: Stata/IC 13.1

Comment

Post Cancel Nick Cox 29 Aug 2014, 02:36 What's the problem? Spaces within filenames. What's the solution? Plain double quotation marks " "

Comment

Post Cancel Konrad Zdeb 29 Aug 2014, 02:47 Thanks, I'm fighting with this. Presently, I have:
* Define path and get CSVs. local importfolder /// `"C:\Users\me\stuff for stata"' di `""`importfolder'*.csv""' global filelist : dir `""`importfolder'*.csv""' invalid syntax

The naming of the files does not adhere to any naming convention, so have spaces capital and small letters and different lengths.

To add to my previous post, it appears that the problem relates to the macro definition:

dir "C:\Users\me\stuff for stata\*.csv" 1853.6k 8/29/14 9:32 Age_24_and_under.csv global files : dir "C:\Users\me\stuff for stata\*.csv" invalid syntax r(198);
Last edited by Konrad Zdeb; 29 Aug 2014, 02:56 . Kind regards,
Konrad
Version: Stata/IC 13.1

Comment

Post Cancel Nick Cox 29 Aug 2014, 03:08

Indeed: your syntax is invalid; it's just fantasy syntax that you would like to work. Stata requires you (1) to separate the directory name and the wildcard pattern and (2) to use the keyword files .

This is documented. Start with help macro .

Comment

Post Cancel Konrad Zdeb 29 Aug 2014, 03:27

I'm trying to follow the advice found here, but it doesn't seem to work. To my mind what is in the filelist doesn't make sense.

* Alternative cd "C:\Users\me\stuff" local filelist: dir . files "*.csv" di `filelist' aged 18-24, claiming for over 6 months.csvage_24_and_under.csv * Import files. foreach datafile of local filelist < preserve insheet using `datafile', clear save temp, replace restore append using temp > invalid '18' r(198); end of do-file

I find it hard to believe that this can't be more simplified, this would take two lines of code in R:

temp <- list. files ( path = "whataver" , pattern = "*.csv" , full . names = T )
myfiles <- lapply ( temp , read . delim )
Last edited by Konrad Zdeb; 29 Aug 2014, 03:38 . Reason: Colours. Kind regards,
Konrad
Version: Stata/IC 13.1

Comment

Post Cancel Nick Cox 29 Aug 2014, 05:30

I think this is exactly the same problem as explained earlier, indeed more than once in this thread.

insheet using "`datafile'", clear

There are spaces in the filename, and you must bind with " ".

I wrote fs (SSC) to make handling lists of files easier.

Comment

Post Cancel Konrad Zdeb 29 Aug 2014, 06:55

Thanks very much for showing interest. I did most of the stuff I wanted, the only outstanding task is to reshape the data with multiple stubs and certain identifiers for time series missing but I'll get there at some point, not that I wouldn't appreciate help. The attached file contains data extract (tab delimited due to forum limitations). For those who may be interested in this, full data sets are available via NOMIS.

/// Import clear cd "C:\path_with_csvs" local filelist: dir . files "*.csv" di `filelist' * Get the base data use "C:\proper_path\sns-igzs.dta", clear keep igz INTGEONAME laname * Import files. foreach datafile of local filelist < preserve import delimited using "`datafile'", clear varnames(1) save temp, replace restore merge 1:1 igz using /// "C:\proper_path\temp.dta", /// nogenerate drop v* destring, replace ignore(-) dropmiss *, force > * remove the temporary .dta file and save compress save jsadta, replace // Renvars renvars, subst(_total) renvars, subst(_tot) renvars, subst(_to) describe // Make panel data *cut out the "_may09 and other" at the end to obtain the actual stubs foreach v of varlist aged1824_oct04 - claimingforover6months_jul14 < local stubs `"`stubs' `=substr("`v'",1,length("`v'")-6)'"' > * Reshape the data to make it long for charts and analysis (doesn't work) reshape long `stubs', i(igz) j(date) * Make proper time series variable (. )
Originally posted by Nick Cox View Post

I wrote fs (SSC) to make handling lists of files easier.