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,
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;
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.
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,
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;
Thank you guys very much. The problem's been solved.
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.
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.
Thank you very much for the info.
Regareds,
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.