Transformation in a loop
Transformation in a loop
Hi,
My problem: I have a number of variables for which I have calculated one-period changes. This yielded positive (some zero) and negative values for the variables. I want to code the negative values into 0's and the non-negative into 1's. Unfortunately the code below produces 1's for all entries and variables:
smpl 2000:2 2012:3
Dofor i = dlprod dlesi dlnord dlppi dlmetmins
set %s('di'+%l(i)) = %if(i.ge.0,1.0,0.0)
end dofor i
However, the below works just fine for one variable
set x = %if(dlprod.ge.0,1.0,0.0)
Any help would be gratefully appreciated. Also any suggestions on how to sum (count) the 1's and 0's over each entry would be very helpful.
Marcus
My problem: I have a number of variables for which I have calculated one-period changes. This yielded positive (some zero) and negative values for the variables. I want to code the negative values into 0's and the non-negative into 1's. Unfortunately the code below produces 1's for all entries and variables:
smpl 2000:2 2012:3
Dofor i = dlprod dlesi dlnord dlppi dlmetmins
set %s('di'+%l(i)) = %if(i.ge.0,1.0,0.0)
end dofor i
However, the below works just fine for one variable
set x = %if(dlprod.ge.0,1.0,0.0)
Any help would be gratefully appreciated. Also any suggestions on how to sum (count) the 1's and 0's over each entry would be very helpful.
Marcus
Re: Transformation in a loop
smpl 2000:2 2012:3
Dofor i = dlprod dlesi dlnord dlppi dlmetmins
set %s('di'+%l(i)) = %if(i.ge.0,1.0,0.0)
end dofor i
The problem is with this expression:
i.ge.0
As written, RATS is going to interpret "i" as referring to the integer-valued identification number associated with the current series, rather than the values stored in the series itself. Because series numbers are always bigger than 0, the expression is always true.
You need to do something to tell RATS to interpret i as a series, rather than as a series number. One way to do that is by adding lag notation (using lag zero since you want to refer to the current entry). For example:
set %s('di'+%l(i)) = %if(i{0}.ge.0,1.0,0.0)
Another way is to use a "type modifier", like this (note the parens around the "[series]i" expression:
set %s('di'+%l(i)) = %if( ([series]i).ge.0,1.0,0.0)
>Also any suggestions on how to sum (count) the 1's and 0's over each entry would be very helpful.
If you know the series will be defined for all entries, you could just use the %sum() function:
compute sum = %sum(%s('di'+%l(i)))
More generally, you can use SSTATS, which computes sums by default:
sstats / %s('di'+%l(i))>>sum1
For example:
dec vector sums(5)
compute n = 1
Dofor i = dlprod dlesi dlnord dlppi dlmetmins
set %s('di'+%l(i)) = %if(i{0}.ge.0,1.0,0.0)
sstats / %s('di'+%l(i))>>sums(n)
compute n = n+1
end dofor i
Regards,
Tom Maycock
Dofor i = dlprod dlesi dlnord dlppi dlmetmins
set %s('di'+%l(i)) = %if(i.ge.0,1.0,0.0)
end dofor i
The problem is with this expression:
i.ge.0
As written, RATS is going to interpret "i" as referring to the integer-valued identification number associated with the current series, rather than the values stored in the series itself. Because series numbers are always bigger than 0, the expression is always true.
You need to do something to tell RATS to interpret i as a series, rather than as a series number. One way to do that is by adding lag notation (using lag zero since you want to refer to the current entry). For example:
set %s('di'+%l(i)) = %if(i{0}.ge.0,1.0,0.0)
Another way is to use a "type modifier", like this (note the parens around the "[series]i" expression:
set %s('di'+%l(i)) = %if( ([series]i).ge.0,1.0,0.0)
>Also any suggestions on how to sum (count) the 1's and 0's over each entry would be very helpful.
If you know the series will be defined for all entries, you could just use the %sum() function:
compute sum = %sum(%s('di'+%l(i)))
More generally, you can use SSTATS, which computes sums by default:
sstats / %s('di'+%l(i))>>sum1
For example:
dec vector sums(5)
compute n = 1
Dofor i = dlprod dlesi dlnord dlppi dlmetmins
set %s('di'+%l(i)) = %if(i{0}.ge.0,1.0,0.0)
sstats / %s('di'+%l(i))>>sums(n)
compute n = n+1
end dofor i
Regards,
Tom Maycock
Re: Transformation in a loop
Thanx,
I didn't explain the summation request properly.
What I need is a sum across entries, i.e. a sum of 1's and 0's for each time period, (a vector with two columns of sums for each period).
In other words, row sums o rather than column sums.
Changed to sums(2) and tried to include a loop over j = 2000:2 2012:3 in the code below but that didn't produce what I wanted. Got two sums instead.
dec vector sums(5)
compute n = 1
Dofor i = dlprod dlesi dlnord dlppi dlmetmins
set %s('di'+%l(i)) = %if(i{0}.ge.0,1.0,0.0)
sstats / %s('di'+%l(i))>>sums(n)
compute n = n+1
end dofor i
Marcus
I didn't explain the summation request properly.
What I need is a sum across entries, i.e. a sum of 1's and 0's for each time period, (a vector with two columns of sums for each period).
In other words, row sums o rather than column sums.
Changed to sums(2) and tried to include a loop over j = 2000:2 2012:3 in the code below but that didn't produce what I wanted. Got two sums instead.
dec vector sums(5)
compute n = 1
Dofor i = dlprod dlesi dlnord dlppi dlmetmins
set %s('di'+%l(i)) = %if(i{0}.ge.0,1.0,0.0)
sstats / %s('di'+%l(i))>>sums(n)
compute n = n+1
end dofor i
Marcus
Re: Transformation in a loop
I'm a bit confused about what you want. Is it that you want to create a series which, at time t, has the number of positive values among the five series that you're listing?
Re: Transformation in a loop
That sounds about right. If three of the series have positive values at 1998:3, the new series, summing 1's over rows, should have a 3 for 1998:3.
Re: Transformation in a loop
This is the simplest way to do that:
Code: Select all
set mycount = 0.0
dofor i = dlprod dlesi dlnord dlppi dlmetmins
set mycount = mycount+(i{0}>=0)
end dofor i