BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Palang
Obsidian | Level 7

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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;

View solution in original post

9 REPLIES 9
Reeza
Super User

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.

 

Palang
Obsidian | Level 7

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,

 

Shmuel
Garnet | Level 18

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;

Palang
Obsidian | Level 7

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

Palang
Obsidian | Level 7

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.

Shmuel
Garnet | Level 18

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. 

Palang
Obsidian | Level 7

Thank you very much for the info.

 

Regareds,

 

ballardw
Super User

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.

 

Palang
Obsidian | Level 7
Thank you very much.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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