Dear Stata Community,
I am quite desperate in need of your wisdom and user power. I have written an ado that is supposed to give the user a detailed overview of every value label in the dataset. Therefore I wrote loops that goes over the whole dataset and each value gathering label information. Clearly, depending on the amount of variables and value label those loops can take their time. Thus, I tried to be smart and was seeking to enhance the processing time by using forvalue loops instead of foreach loops (z is the amount of values in a variable)
The difficulty is that some variables can contain only one value and therefore the forvalue loop would result in an error because Stata cannot execute
Fortunately, the foreach loop is able to loop over single numbers by typing
. Now, if z would be 1 the loop would be executed without an error. So I thought instead of writing "if commands" that control for z being higher than one and to duplicate the following syntax I used a macro to manipulate the loop command (I am using tempnames for z and loop_type and a lot of other locals in my ado thats why there are so many quotation marks):
The strange thing is that this code does not work while a version only containing a foreach loop just runs fine. Using the trace option I found out that the first foreach loop which runs over all variables seems to loose the variable stored in `var' during one of the loops. At first I thought I missed a curled bracket or added one by accident, but a comparison to the earlier working version with a text editor showed me that there are no extra or missing brackets and Stata does not give me the too few ({ error.
I assume that somehow the execution of the macros lead temporary to a bracket closing the foreach var of varlist _all loop and therefore terminating the content of `var' But I really don't know if this is the case and to look at the syntax and not seeing any error drives me nuts.
Maybe some more experienced user or someone from StataCorp can help me out here and has an idea what is happening.
Best wishes and thank you very much in advance,
Malte
PS: For more clarity on the subject I attached the whole syntax.
I am quite desperate in need of your wisdom and user power. I have written an ado that is supposed to give the user a detailed overview of every value label in the dataset. Therefore I wrote loops that goes over the whole dataset and each value gathering label information. Clearly, depending on the amount of variables and value label those loops can take their time. Thus, I tried to be smart and was seeking to enhance the processing time by using forvalue loops instead of foreach loops (z is the amount of values in a variable)
Code:
(forvalue x = 1/z)
Code:
forvalue x = 1/1
Code:
foreach x of numlist 1/z
Code:
foreach var of varlist _all { syntax ... if ``z'' == 1 { local `loop_type' = `"foreach x of numlist ``z''"' } else { local `loop_type' = `"forval x = ``z''"' } ``loop_type''/``z'' { syntax... } syntax... }
I assume that somehow the execution of the macros lead temporary to a bracket closing the foreach var of varlist _all loop and therefore terminating the content of `var' But I really don't know if this is the case and to look at the syntax and not seeing any error drives me nuts.
Maybe some more experienced user or someone from StataCorp can help me out here and has an idea what is happening.
Best wishes and thank you very much in advance,
Malte
PS: For more clarity on the subject I attached the whole syntax.
Comment