What does w{%A}(!i+1) mean in EViews? - eviews

Couldn't find any hint about w{} on eviews documents. Any explanation?
w{}(), can't understand how it works.
BTW, how can I print a single variable to command window not print it to a file?
Thanks!

Curly braces in EViews work the way that statements like eval() perform in other programming languages. They tell EViews "strip quotes off of this string and evaluate it as valid EViews code".
%A is a "program string", a temporary string variable only used when executing an EViews program. !i is a "program scalar", a temporary scalar variable only used when executing an EViews program.
It's hard to answer your question without knowing what type of object W{%A} is supposed to be. But if %A holds a value like USA, for example, W{%A} will look for an object in your workfile called WUSA. If !i holds the value 1, for example, W{%A}(!i+1) is equivalent to WUSA(2). This could, for example, be an attempt to grab a particular element of a vector object.

{} can change a variables value (in programming not series) to string. For example if there is WTR, WCR, KJ and Y series in our file, following command would be run.
%A = "TR"
!i=2
ls Y c W{%A}(!i+1)
Means ls Y c WTR(3) and also
%A = "TR"
%B= "CR"
%F="KJ"
!i=3
!k=-1
ls Y c W{%A}(!i+1) W{%B}(!k-3) {%F}
Means ls Y c WTR(4) WCR(-4) KJ

Related

Assign value to local variable with if-statement

I'm trying to assign a conditional value to a local macro variable in Stata 15.
I have a local variable that only can have two values; "o" or "u". Then I have another local variable that I want to get the other letter of these two than the first local variable.
My code looks like this:
local utr o /*Can be assigned either "o" or "u".*/
local uin u if `utr' == o
local uin o if `utr' == u
di "utr = `utr'"
di "uin = `uin'"
I've also tried a number of variations of this code where I only have one "=" in the if statement and have had "" around the letters in the conditional statements.
I get a error messages that says:
if not allowed
so I guess I can´t do it like this if it´s possible at all.
Is it at all possible to assign "automated" conditional local variable values in Stata?
And if it is possible, how should I do this?
Local macros are not variables; these two are distinct in Stata.
The following works for me:
local utr o // can be assigned either "o" or "u"
if "`utr'" == "o" local uin u
else local uin o
display "utr = `utr'"
utr = o
display "uin = `uin'"
uin = u
See this page for an explanation on the difference between the if command and the if qualifier.
Let's focus on the if qualifier not being allowed in the definition of a local macro. This is a supplement to #Pearly Spencer's fine answer and not an alternative to it.
First off, the syntax diagram for the local command (e.g. help local gets you there) doesn't show that it's allowed. That almost always means that it really is forbidden. (Very occasionally, there are undocumented details to syntax.)
Second, and more to the point, there is no reason for an if qualifier here. An if qualifier allows for different results depending on a subset of observations, but local macros have nothing to do with the dataset strict sense. They apply equally to all observations or indeed to none.
None of that denies that a programmer, like you, often wants to define local macros conditional on something else, and that calls for something else, such as the if command or cond().

How do I return the value after checking for a condition?

The following code has to check for an 'e' value such that the gcd(h,e)=1. Where 1
module great(p,q,e,d);
input p,q;
output e,d;
reg e,d;
h=((p-1)*(q-1));
always
begin
for(e=2;e<h;e=e+1)
begin
g1=gcd(h,e);
if(g1==1)
return e;
If, by "return a value", you mean spit out a value you can use in another module, you would use the output of this module as your "return" value. But even ignoring the return e, I don't think your code will work if you tried to run it, because it is too much like a programming language. There's several major things wrong:
You already declared output e,d so you can't declare two reg with the same name. You probably want output reg e,d instead.
You didn't declare a type for h or g1.
You have a for loop for e but e can never be anything other than 0 or 1 because you didn't set a size for it, so by default it is only 1-bit long. Even if it was big enough that you could increment it past 1, it's a wire type by default, and you can't make those kind of increments to a wire directly.
I assume gcd is some module you made somewhere else, but this isn't how you interconnect modules together. You can't call it like it's a function. You have to use wire and reg to connect the inputs and outputs of two modules together, almost like you're plugging components in.
Those are what stick out the most to me, anyway. I think you are coding your Verilog as if it were Python and that's what's causing these misunderstandings. Verilog is very, very different.

TCL: check if variable is list

set var1 A
set var2 {A}
Is it possible to check if variable is list in TCL? For var1 and var2 llength gives 1. I am thinking that these 2 variables are considered same. They are both lists with 1 element. Am I right?
Those two things are considered to be entirely identical, and will produce identical bytecode (except for any byte offsets used for indicating where the content of constants are location, which is not information normally exposed to scripts at all so you can ignore it, plus the obvious differences due to variable names). Semantically, braces are a quoting mechanism and not an indicator of a list (or a script, or …)
You need to write your code to not assume that it can look things up by inspecting the type of a value. The type of 123 could be many different things, such as an integer, a list (of length 1), a unicode string or a command name. Tcl's semantics are based on you not asking what the type of a value is, but rather just using commands and having them coerce the values to the right type as required. Tcl's different to many other languages in this regard.
Because of this different approach, it's not easy to answer questions about this in general: the answers get too long with all the different possible cases to be considered in general yet most of it will be irrelevant to what you're really seeking to do. Ask about something specific though, and we'll be able to tell you much more easily.
You can try string is list $var1 but that will accept both of these forms - it will only return false on something that can't syntactically be interpreted as a list, eg. because there is an unmatched bracket like "aa { bb".

can't evaluate if statement with variables

I've got experience in a lot of other programming languages, but I'm having a lot of difficulty with Stata syntax. I've got a statement that evaluates with no problem if I put in values, but I can't figure out why it's not evaluating variables like I expect it to.
gen j=5
forvalues i = 1(1)5 {
replace TrustBusiness_local=`i' if TrustBusiness_local2==`j'
replace j=`j'-1
}
If I replace i and j with 1 and 5 respectively, like I'm expecting to happen from the code above, then it works fine, but I get an if not found error otherwise, which hasn't produced meaningful results when Googled. Does anyone see what I don't see? I hate to brute-force something that could so simply be done with a loop.
Easy to understand once you approach it the right way!
Problem 1. You never defined local macro j. That in itself is not an error, but it often leads to errors. Macros that don't exist are equivalent to empty strings, so Stata sees in this example the code
if TrustBusiness_local2==`j'
as
if TrustBusiness_local2==
which is illegal; hence the error message.
Problem 2. There is no connection of principle between a variable you called j and a local macro called j but referenced using single quotes. A variable in Stata is a variable (namely, column) in your dataset; that doesn't mean a variable otherwise in the sense of any programming language. Variables meaning single values can be held in Stata within scalars or within macros. Putting a constant into a variable, Stata sense, is legal, but usually bad style. If you have millions of observations, for example, you now have a column j with millions of values of 5 within it.
Problem 3. You could, legally, go
local j "j"
so that now the local macro j contains the text "j", which depending on how you use it could be interpreted as a variable name. It's hard to see why you would want to do that here, but it would be legal.
Problem 4. Your whole example doesn't even need a loop as it appears to mean
replace TrustBusiness_local= 6 - TrustBusiness_local2 if inlist(TrustBusiness_local2, 1,2,3,4,5)
and, depending on your data, the if qualifier could be redundant. Flipping 5(1)1 to 1(1)5 is just a matter of subtracting from 6.
Problem 5. Your example written as a loop in Stata style could be
local j = 5
forvalues i = 1/5 {
replace TrustBusiness_local=`i' if TrustBusiness_local2==`j'
local j=`j'-1
}
and it could be made more concise, but given Problem 4 that no loop is needed, I will leave it there.
Problem 6. What you talking about are, incidentally, not if statements so far as Stata is concerned, as the if qualifier used in your examples is not the same as the if command.
The problem of translating one language's jargon into another can be challenging. See my comments at http://www.stata.com/statalist/archive/2008-08/msg01258.html After experience in other languages, the macro manipulations of Stata seemed at first strange to me too; they are perhaps best understood as equivalent to shell programming.
I wouldn't try to learn Stata by Googling. Read [U] from beginning to end. (A similar point was made in the reply to your previous question at use value label in if command in Stata but you don't want to believe it!)

Check the input data type and whether it is empty or null

I am asking user to give a value at run time to do some calculations.
I want to test if the user entered value is a real/integer number, and if not then give a warning that the program is expecting a real/integer number here.
In addition, I would as well like to know how do we check if a particular variable at the moment is null or empty. i.e. I have declared a variable but what if at the time of calculation its value is null or empty or not yet set, in that case, the program shouldn't crash instead give a warning to provide a correct value.
Both these operations are much more easier in C++ and C#, but I couldn't find a way to do that in Fortran.
I guess that "null or empty" you mean whether a variable has been initialized: "not yet set". "null" has a particular meaning for Fortran pointer variables, but I suppose that this is not your question.
Fortran doesn't automatically give variables a special value before they are intentionally initialized so there is no easy way to check whether a variable has been initialized. One approach is to initialize the variable its declaration with a special value. That means that you need to know a special value that it will never obtain in the operation of the program. One possibility is to use the huge intrinsic:
program TestVar
real :: AVar = huge (1.0)
if ( AVar < huge (1.0) ) then
write (*, *) "Test 1: Good"
else
write (*, *) "Test 1: Bad"
end if
AVar = 2.2
if ( AVar < huge (1.0) ) then
write (*, *) "Test 2: Good"
else
write (*, *) "Test 2: Bad"
end if
end program TestVar
As warned by #arbautjc, this only works once, even in a subroutine. In a procedure, the initialization with declaration is only done with a first call. Also, if you change the variable type from this example, be sure to understand how huge works (e.g., Long ints in Fortran).