11-14-2016 02:58 PM - edited 11-14-2016 03:16 PM
Hello,
I need to compare the valus of a variable to understrand at what points thers is a significant change.
For example:
Date | SRAD | |
06/12/2013 | 250 | |
07/12/2013 | 20 | |
08/12/2013 | 160 | |
09/12/2013 | 17 | |
10/12/2013 | 120 |
I would need to do this:
do i=1 to 500000
"if SRAD(i+1)<0.3*SRAD(i) or SRAD(i+1)>1.7*SRAD(i) then x=1"
It'd be appracited if you let me know how I can perform this comparison in SAS.
Thank you,
11-14-2016 03:44 PM - edited 11-14-2016 03:46 PM
Try next code:
data want;
set have;
retain prev_srad;
if _N_=1 then prev_srad = srad;
relation = srad / prev_srad ;
if relation le 0.3 or relation ge 1.7 then x=1; else x=0;
output;
prev_srad = srad;
drop prev_srad;
run;
11-14-2016 03:22 PM
Are you trying to look forward or look back? Where do you want that result to go and does it matter?
FYI - looking back is easier than looking forward in SAS.
Please post what you would expect as output to help clarify your problem.
11-14-2016 03:32 PM - edited 11-14-2016 03:32 PM
Thank you for the response. I got a time series dataset with 5 min resolution, and wanna spot the points at which solar radiation is suddenly decreased/increased by about %70. To do so, I want to compare the solar radiation quantities consecutively to spot the points of interest, and delete the rows at which this sudden increase/decrease doesn't happen. I think the code should look forward.
Comparing the rows like this and set new variable "x" to 1 if the if statement is satisfied, o.w. x=0. Then, I can delete the rows with x=0.
if SRAD(i+1)<0.3*SRAD(i) then x=1;
Thank you,
11-14-2016 03:44 PM - edited 11-14-2016 03:46 PM
Try next code:
data want;
set have;
retain prev_srad;
if _N_=1 then prev_srad = srad;
relation = srad / prev_srad ;
if relation le 0.3 or relation ge 1.7 then x=1; else x=0;
output;
prev_srad = srad;
drop prev_srad;
run;
11-14-2016 03:56 PM
Thank you guys very much. The problem's been solved.
11-14-2016 04:43 PM
Thank you very much. The problem's been solved. Could you please shade some ligh on the prev_srad temporary variable and how does the itteration is performed?
Thank you much.
11-14-2016 08:04 PM
You can add new variables to output just by naming it and assigning it a value,
like in:
data test;
infile datalines;
input var1 var2;
new_var = var1 + var2;
datalines;
10 5
20 3
;
run;
in this case new_var is initiated again and again in eatch iteration.
using retain enables keep value through iterations, without to be initiated at new input comming.
11-14-2016 10:45 PM
Thank you very much for the info.
Regareds,
11-16-2016 04:18 PM
Minor difference code using the LAG function
data want;
set have;
relation = srad / lag(srad) ;
if relation le 0.3 or relation ge 1.7 then x=1;
else x=0;
run;
LAG(variablename) allows you to examine values of a variable on previous rows of a data set. Use Lag2 to look at the record 2 before the current, Lag3 the record 3 before the current and so on. There are some tricky elements so it is best not to use LAG, or the related function DIF, in conditional statements.
11-16-2016 05:07 PM
Need further help from the community? Please ask a new question.