## Comparing values of a variable

Solved
Occasional Contributor
Posts: 13

# Comparing values of a variable

[ Edited ]

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,

Accepted Solutions
Solution
‎11-14-2016 03:55 PM
Trusted Advisor
Posts: 1,837

## Re: Comparing values of a variable

[ Edited ]

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;

All Replies
Super User
Posts: 23,776

## Re: Comparing values of a variable

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.

Occasional Contributor
Posts: 13

## Re: Comparing values of a variable

[ Edited ]

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,

Solution
‎11-14-2016 03:55 PM
Trusted Advisor
Posts: 1,837

## Re: Comparing values of a variable

[ Edited ]

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;

Occasional Contributor
Posts: 13

## Re: Comparing values of a variable

Thank you guys very much. The problem's been solved.

Occasional Contributor
Posts: 13

## Re: Comparing values of a variable

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.

Trusted Advisor
Posts: 1,837

## Re: Comparing values of a variable

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.

Occasional Contributor
Posts: 13

## Re: Comparing values of a variable

Thank you very much for the info.

Regareds,

Super User
Posts: 13,583

## Re: Comparing values of a variable

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.

Occasional Contributor
Posts: 13

## Re: Comparing values of a variable

Thank you very much.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 9 replies
• 524 views
• 3 likes
• 4 in conversation