Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:43:10+00:00 2026-06-02T00:43:10+00:00

The following bit of SAS code is supposed to read from a dataset which

  • 0

The following bit of SAS code is supposed to read from a dataset which contains a numeric variable called ‘Radvalue’. Radvalue is the temperature of a radiator, and if a radiator is switched off but then its temperature increases by 2 or more it’s a sign that it has come on, and if it is on but its temperature decreases by 2 or more it’s a sign that it’s gone off.
Radstate is a new variable in the dataset which indicates for every observation whether the radiator is on or off, and it’s this I’m trying to fill in automatically for the whole dataset.
So I’m trying to use the LAG function, trying to initialise the first row, which doesn’t have a dif_radvalue, and then trying to apply the algorithm I just described to row 2 onwards.
Any idea why the columns Radstate and l_radstate come out completely blank?

Thanks everso much!! Let me know if I haven’t explained the problem clearly.

Data work.heating_algorithm_b;
 Input ID Radvalue; 
 Datalines; 
  1 15.38 
  2 15.38 
  3 20.79 
  4 33.47 
  5 37.03 
  6 40.45 
  7 40.45 
  8 40.96 
  9 39.44 
  10 31.41 
  11 26.49 
  12 23.06 
  13 21.75 
  14 20.16 
  15 19.23 
 ; 

DATA temp.heating_algorithm_c;
 SET temp.heating_algorithm_b;

 DIF_Radvalue = Radvalue - lag(Radvalue);

 l_Radstate = lag(Radstate);

 if missing(dif_radvalue) then  
  do;
   dif_radvalue = 0;
   radstate = "off"; 
  end;                            
 else if l_Radstate = "off"  &  DIF_Radvalue > 2    then Radstate = "on";
 else if l_Radstate = "on" &  DIF_Radvalue < -2  then  Radstate = "off";
 else  Radstate = l_Radstate;
run;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:43:11+00:00Added an answer on June 2, 2026 at 12:43 am

    You were trying to perform the LAG function on a variable only existing in the output data set (RADSTATE). I replaced the LAG on RADSTATE with a RETAIN. Also, you were right to keep the LAG function outside any conditional logic…Try the below code.

    Data work.heating_algorithm_b;
     Input ID Radvalue; 
     Datalines; 
      1 15.38 
      2 15.38 
      3 20.79 
      4 33.47 
      5 37.03 
      6 40.45 
      7 40.45 
      8 40.96 
      9 39.44 
      10 31.41 
      11 26.49 
      12 23.06 
      13 21.75 
      14 20.16 
      15 19.23 
     ; 
    
    DATA work.heating_algorithm_c;
     length radstate $3;
     retain radstate;
     SET work.heating_algorithm_b;
    
     old_radvalue=lag(radvalue);
    
     if _n_=1 then do;
      dif_radvalue=0;
      radstate="off";
     end;
     else do;
      DIF_Radvalue = Radvalue-Old_Radvalue;
    
      if Radstate = "off"  &  DIF_Radvalue > 2    then Radstate = "on";
      else if Radstate = "on" &  DIF_Radvalue < -2  then  Radstate = "off";
      /* Else Radstate stays the same */
     end;
    run;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following bit of code in a method called by clicking the
I have the following bit of code which is responsible for displaying a tooltip.
I have the following bit of code: $query = SELECT * FROM jos_vm_product_details WHERE
I've got the following bit of code, which I've narrowed down to be causing
I have the following bit of code: As a global variable: char *orderFiles[10]; And
I have the following bit of code which I'm using to delete all of
The following bit of code populates a class that inherits from the wx.Menu class,
I have the following bit of code that reads data from the an Oracle
Why is the following bit of code returns Height: -1 which means that the
I have the following bit of PHP code, which creates three database tables, then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.