Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • using a scalar in a filename when saving a file

    I have many stata data files (originally from excel spreadsheets) I am trying to organize into a useable data set
    Each of the stata data files has data on about 50 cities (the observations) and the results of 1 to 6 simulations. Each simulation gives multiple results (variables) and takes multiple parameters.
    I would like to make a file for each simulation. Variables on the data set describe the simulations (e.g. a $200K house in 2006 with no assessment limit, this is the same across all cities). I would like to embed this description in the newly-saved file name. Unfortunately it is not easy to put this in a loop (or use the reshape command) since some simulations have more results than others

    My (unsatisfactory) code thus far is (generically) [I've simplified the names of the variables and parameters here]
    The problem is that the command save `file_name1' names the saved file a name like "file __000004.dta" rather than the text assigned to the scalar.

    use "top50 home 2005.dta", clear
    preserve
    generate simulation_name=B
    keep simulation_name State City result1 result2 param1 param2 param3
    tempname file_name1;
    scalar `file_name'=B[1];
    save `file_name1'
    restore, preserve
    generate simulation_name=H
    keep simulation_name State City result3 result4 param4 param5 param6
    tempname file_name2;
    scalar `file_name'=H[1];
    save `file_name2'

    Thanks for any help.






  • #2
    It's difficult here to follow what you want to do.

    0. You are arbitrarily flipping between using semi-colons to end commands and end-of-lines.

    1. Your assignments to a new variable simulation_name would work if and only if you have variables named B and H. We have no way of checking that so far as I can see.

    2. However, the references to H[1] and B[1] certainly make no sense, as after each keep no such variables exist. So, you need to refer to the variable you just created, not the variable you just discarded. (No variables, no scope for extracting values by subscripting.) Alternatively, you should use that value before you drop the variable (i.e. don't keep it).

    3. Also there are bugs in your references to a scalar with temporary name file_name. You never asked for any such name.
    I think you are getting confused between scalars and local macros.

    The code below optimistically corrects for what I think I understand to be wrong.

    Code:
    use "top50 home 2005.dta", clear
    preserve
    generate simulation_name=B
    keep simulation_name State City result1 result2 param1 param2 param3
    local file_name = sim[1]
    save `file_name'
    restore, preserve
    
    generate simulation_name=H
    keep simulation_name State City result3 result4 param4 param5 param6
    local file_name = sim[1]
    save `file_name'
    All that said, saving to lots of files usually strikes me as making data management and analysis more difficult. If I wanted to compare simulations, and I always do, I'd want to keep results in the same file. But I'd need different simulations to be different blocks of observations. In short, my one-word suggestion for a strategy here is reshape.


    Comment


    • #3
      Thanks for bearing with me.

      I have now got my program to work with (essentially) your code. I very much appreciate the help.

      1. Sorry for the sloppiness with the end of line commands...I meant them all to be semi-colons
      2. B is actually a variable that describes the simulation (so the same for all 50 cities) and it was saved in my original code but I failed to communicate that in my post. B[1] was meant to refer to the value of B at the first observation and yes I imagine I was confused about scalars and local macros.

      I agree this is a data management nightmare. I wish I could do it with the reshape command and have spent hours puzzling over that but for reasons stated in my original post can't figure out how to do it. I did not create the original simulations or name the variables--I got them in excel files--and have had to take it from there.

      Thanks again.

      Comment


      • #4
        Good to hear of progress. You will appreciate that we can comment only on what you post.
        Last edited by Nick Cox; 04 Nov 2014, 13:04.

        Comment

        Working...
        X