I would like to write a macro that would allow me to import and manipulate data based on values of variable in an initial dataset (but nothing I have tried so far works due to my ignorance of stata coding). As an example, for the following dataset:
I would like to import files based on values of the variable fruit , then generate variables (and manipulate them) based on the values of the variable locus, and finally save those files using filenames that depend on the values of variable site.
My two big stumbling blocks are (1) how to keep the values of the variable in the initial dataset in some type of memory that can be accessed later and (2) how to refer to the values of the variables in the macro.
I tried the commands below but the clear command in the foreach loop is obviously a death knell.
It looks like levelsof could be a useful tool in this case, but I can't figure how to use that for three different variables at one time. Suggestions for how to deal with this would be much appreciated!
fruit | site | locus |
apples | a | 1 |
orange | b | 2 |
bananas | c | 3 |
My two big stumbling blocks are (1) how to keep the values of the variable in the initial dataset in some type of memory that can be accessed later and (2) how to refer to the values of the variables in the macro.
I tried the commands below but the clear command in the foreach loop is obviously a death knell.
Code:
use data, clear levelsof fruit, local(levels) foreach rep of local levels { clear insheet using "`apples'.txt" gen locus_2=`locus'+1 save "`apples'_`site'.dta", replace }