I am working with string data and I would like to import multiple Excel files and append them, creating in each of them a string variable whose unique value is the name of the corresponding file (so that I can identify from which file each set of observations come).
In particular, in the working directory I have 56 files named as follows: 'NAME1.xlsx', 'NAME2.xlsx', ..., 'NAME56.xlsx' where 'NAME?' represents any word with length <= 32.
I would like the variable for file 'NAME1.xlsx' to have only the value 'NAME1' for all observations of the 'NAME1.dta' file (which I will save interactively in a loop).
wanted_var | var2 | var3 |
NAME1 | etc | ... |
NAME1 | etc | ... |
NAME1 | etc | ... |
The same would be done for the file 'NAME2.dta' and so forth for all the 56 files
wanted_var | var2 | var3 |
NAME2 | etc | ... |
NAME2 | etc | ... |
NAME2 | etc | ... |
In the final version of the appended dataset, I would also like to add an empty row (i.e., a new observation) after each set of observations that come from each file. [I tried to use something like 'insobs 1, after(`wanted_var[_n] != wanted_var[_n-1]')]
The dataset ideally would be like
wanted_var | var2 |
NAME1 | etc |
NAME1 | etc |
NAME1 | etc |
(empty row) | etc |
NAME2 | etc |
NAME2 | etc |
(empty row) | etc |
NAME3 | etc |
NAME3 | etc |
NAME3 | etc |
NAME3 | etc |
NAME3 | etc |
(empty row) | etc |
NAME4 | etc |
NAME4 | etc |
(and so on) | .... |
Can you help to find a solution for that?
Thank you very much!
0 Response to How to create a string variable whose unique value is the name of a file in the directory and add new observations interactively
Post a Comment