Hello
This is maybe a stupid question, however, I have tried hard to answer it without using the time of the Stata Forum. I have just started using Stata and would like to write little programs as I do in other languages, such as pascal, SAS or R. Unfortunately, I seem to have a fundamental misconception about the workings of Stata.
One of the most basic tools for programming is the if-else facility (with all its extensions and variations, such as while etc.).
The problem is the following: whenever I try to use the if command, the commands specified after the { are not executed even though the specified condition is met in a given dataline (condition evaluates to "true"), as it would be the case in other programming languages. I searched in the FAQ files and found an answer, which I do not understand, however (underlined words, in particular). I quote
"A common mistake is to use the if command as the argument. This is incorrect Stata syntax. The if command was designed to be used with a single expression (often a local macro) inside programs and do-files. Using this command incorrectly results in the evaluation of the expression using only the first observation of the variable
Example: there is a simple datafile with the variable x with values 1 1 2 3 2 .... With the following programm lines
if x == 3 {
replace x = 4
}
no changes are made. If the condition is "if x==1" instead, all values of x in the dataset are replaced with 4, because the condition is met for the first data line.
FAQ says:
"This is obviously not what the user intended. The solution to this problem is often to rewrite the command using if as a qualifier rather than as a command. As a qualifier, if will evaluate each observation as the command passes over the data." such as
replace x = 4 if x == 3
replace x = 4 if x == 1
I cannot believe that this should be the only solution to the problem. The if qualifier is much less flexible and does not allow constructions with else. In other words: the if command would appear as quite useless to me if it were not executed for each line in a dataset (as I know it from other programming languages).
Thank you very much in advance for the right hint
Martin
This is maybe a stupid question, however, I have tried hard to answer it without using the time of the Stata Forum. I have just started using Stata and would like to write little programs as I do in other languages, such as pascal, SAS or R. Unfortunately, I seem to have a fundamental misconception about the workings of Stata.
One of the most basic tools for programming is the if-else facility (with all its extensions and variations, such as while etc.).
The problem is the following: whenever I try to use the if command, the commands specified after the { are not executed even though the specified condition is met in a given dataline (condition evaluates to "true"), as it would be the case in other programming languages. I searched in the FAQ files and found an answer, which I do not understand, however (underlined words, in particular). I quote
"A common mistake is to use the if command as the argument. This is incorrect Stata syntax. The if command was designed to be used with a single expression (often a local macro) inside programs and do-files. Using this command incorrectly results in the evaluation of the expression using only the first observation of the variable
Example: there is a simple datafile with the variable x with values 1 1 2 3 2 .... With the following programm lines
if x == 3 {
replace x = 4
}
no changes are made. If the condition is "if x==1" instead, all values of x in the dataset are replaced with 4, because the condition is met for the first data line.
FAQ says:
"This is obviously not what the user intended. The solution to this problem is often to rewrite the command using if as a qualifier rather than as a command. As a qualifier, if will evaluate each observation as the command passes over the data." such as
replace x = 4 if x == 3
replace x = 4 if x == 1
I cannot believe that this should be the only solution to the problem. The if qualifier is much less flexible and does not allow constructions with else. In other words: the if command would appear as quite useless to me if it were not executed for each line in a dataset (as I know it from other programming languages).
Thank you very much in advance for the right hint
Martin
Comment