y**3 发帖数: 267 | 1 help needed from sas expert!
I have 2 variables in my sas data set-var1 var2. var2 only has value at the
first record.i need to compute the rest of var2 based on the ratios of two
records next to each other; for ex;
the 2nd row ; var22=(95/87)*85; then compute var23 based on the new number
and var12 and var13.
how to code this in sas efficiently? thanks
var1 var2
87 95
85
78
75
65
56
34 |
a******r 发帖数: 8 | 2 data new;
set old;
if (_n_ EQ 1) then tem=var2/var1;
retain tem;
var2=int(var1*tem);
keep var1 var2;
run;
the
【在 y**3 的大作中提到】 : help needed from sas expert! : I have 2 variables in my sas data set-var1 var2. var2 only has value at the : first record.i need to compute the rest of var2 based on the ratios of two : records next to each other; for ex; : the 2nd row ; var22=(95/87)*85; then compute var23 based on the new number : and var12 and var13. : how to code this in sas efficiently? thanks : var1 var2 : 87 95 : 85
|
y**3 发帖数: 267 | 3 Thanks for the help!
But this way tem was set only var2/var1 at _n_=1. that is, tem is same for
all the rest of rows. But I need to re compute tem using the new var2 for
every rows. A do loop work?
【在 a******r 的大作中提到】 : data new; : set old; : if (_n_ EQ 1) then tem=var2/var1; : retain tem; : var2=int(var1*tem); : keep var1 var2; : run; : : the
|
a******r 发帖数: 8 | 4 According to your description, "tem=var2/var1" should have the same value
for all lines, right?
【在 y**3 的大作中提到】 : Thanks for the help! : But this way tem was set only var2/var1 at _n_=1. that is, tem is same for : all the rest of rows. But I need to re compute tem using the new var2 for : every rows. A do loop work?
|
a******r 发帖数: 8 | 5 If you were not sure, try the following one.
data new;
set old;
if (_n_ EQ 1) then tem=var2/var1;
var2=var1*tem;
tem=var2/var1;
retain tem;
keep var1 var2;
run;
Be careful about the type of variables if your original types of var1 and
var2 are both integer.
【在 y**3 的大作中提到】 : Thanks for the help! : But this way tem was set only var2/var1 at _n_=1. that is, tem is same for : all the rest of rows. But I need to re compute tem using the new var2 for : every rows. A do loop work?
|
y**3 发帖数: 267 | 6 Good job! thanks !!!!!!!!!!!!!
【在 a******r 的大作中提到】 : If you were not sure, try the following one. : data new; : set old; : if (_n_ EQ 1) then tem=var2/var1; : var2=var1*tem; : tem=var2/var1; : retain tem; : keep var1 var2; : run; : Be careful about the type of variables if your original types of var1 and
|
a******c 发帖数: 291 | 7 Thanks a lot. I encountered a similar question half a year ago and used a
very clumsy method to resolve it by stacking computed rows one after another
. This is much more efficient. |
a******c 发帖数: 291 | 8 Thanks a lot. I encountered a similar question half a year ago and used a
very clumsy method to resolve it by stacking computed rows one after another
. This is much more efficient. |