English 中文(简体)
长期合作行动的职能存在更多的问题
原标题:more problems with the LAG function is SAS

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;
最佳回答

你们正试图在产出数据组(RADSTATE)现有的变量上履行LAG职能。 我用一个RETAIN取代了关于RAASTATE的LAG。 另外,你也有权不附带任何条件逻辑。 采用以下代码。

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;
问题回答

我没有待命安排制度的经验,但也许需要一份<条码>(l_Radstate)的核对,以便第一次通过,例如:

if missing(l_Radstate) then
do; radstate = "off"; end; 

我认为,只有在以下情况下才需要这样做:Rad Value - lag(Rad Value)没有强迫DIF_Rad Value。 如果是的话,我不相信会帮助......。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签