st: RE: Creating a variable from comments/header in a .txt file

Dear Nick,
Thank you for your reply.
I now understand that pl[1] or varname[1] refers to the contents of the varname. I was able to successfully replace the redundant macros in the first half of the do file and save the file using the contents variable pl. . . .
infile using eli2.dct, clear
. .
gen pl=substr(plate,18,6) /* this is data from the first line : in this case "10u5uc"*/
. .
save `=pl[1]', replace
*this then saves the data as 10u5uc.dta

However, I need to read in another set of data, and it appears that the reference to pl[1] disappears
merge obsno using "`=pl[1]'"
pl not found
invalid file specification
r(198);

Is there a way to save as a global macro and then delete it after I am done with it?
Juan

---
I'll try the easier part of this. Once your filename is in pl[1]
you can refer to that on the fly by `=pl[1]'
so your -merge- command would then be

I have made a do file that can read the tabular data beginning in Line 11 contained in data.txt using infile and a data dictionary. My problem is that I want to tag each observation with a unique identifier that would be taken from line 1.

The solution I tried seems very clumsy, and it doesn't actually work. I created a separate file with a variable (pl) containing the unique file identifier (the string from Line 1). I then generated an observation no using generate obsno=_n and saved the file with the filename as the string from line 1. This file can then be merged with the tabular data .

- start of do file -
set more off
infile using eli2.dct, clear
drop in 2/l
gen pl=substr(plate,18,6) /* this is data from the first line for example a string "10u5uc"*/
gen obsno=_n
local file =pl
sort obsno
save `file', replace
infile using eli.dct, clear
gen obsno=_n
sort obsno
merge obsno using 10u5uc, keep(pl) /* How do I do this using a macro instead of typing "10u5uc"

- end of do file

The resulting merged file then contains 8 observations and a string variable pl. As expected, variable pl is missing for records 2-8. I would like variable pl for records 2-8 to have the same value as record 1. I can do this manually:

replace pl ="10u5uc" if _merge==1

but would rather that it were a macro.

1. How do I extract the data in Line 1 into a variable and repeat this for all the records?
2. Is it possible to merge files and refer to the using dataset using a macro?