在 SAS 中使用 Retain 语句进行数学运算

发布于 2024-07-24 07:31:18 字数 151 浏览 5 评论 0原文

我有一个数据集,每人有 4 个观察值(行)。

我想创建三个新变量来计算第二行和第一行、第三行和第二行、第四行和第三行之间的差异。

我认为保留可以做到这一点,但我不确定如何做到。

或者我需要一个数组吗?

谢谢!

I have a dataset with 4 observations (rows) per person.

I want to create three new variables that calculate the difference between the second and first, third and second, and fourth and third rows.

I think retain can do this, but I'm not sure how.

Or do I need an array?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

菊凝晚露 2024-07-31 07:31:19

data test;
  input person var;
  datalines;
1 5
1 10
1 12
1 20
2 1
2 3
2 5
2 90
;
run;

data test;
  set test;
  by person notsorted;
  retain pos;
  array diffs{*} diff0-diff3;
  retain diff0-diff3;
  if first.person then do;
    pos = 0;
  end;
  pos + 1;
  diffs{pos} = dif(var);
  if last.person then output;
  drop var diff0 pos;
run;


data test;
  input person var;
  datalines;
1 5
1 10
1 12
1 20
2 1
2 3
2 5
2 90
;
run;

data test;
  set test;
  by person notsorted;
  retain pos;
  array diffs{*} diff0-diff3;
  retain diff0-diff3;
  if first.person then do;
    pos = 0;
  end;
  pos + 1;
  diffs{pos} = dif(var);
  if last.person then output;
  drop var diff0 pos;
run;

清欢 2024-07-31 07:31:19

为什么不使用滞后函数。

data test;  input person var;
cards; 
1 5
1 10
1 12
1 20
2 1
2 3
2 5
2 90
run;

data test;  set test;
by person; 
LagVar=Lag(Var);
difference=var-Lagvar;
if first.person then difference=.;
run;

Why not use The Lag function.

data test;  input person var;
cards; 
1 5
1 10
1 12
1 20
2 1
2 3
2 5
2 90
run;

data test;  set test;
by person; 
LagVar=Lag(Var);
difference=var-Lagvar;
if first.person then difference=.;
run;
一绘本一梦想 2024-07-31 07:31:19

另一种不使用数组的方法。

/*-- Data from simonn's answer --*/
data SO1019005;
  input person var;
  datalines;
1 5
1 10
1 12
1 20
2 1
2 3
2 5
2 90
;
run;

/*-- Why not just do a transpose? --*/
proc transpose data=SO1019005 out=NewData;
by person;
run;

/*-- Now calculate your new vars --*/
data NewDataWithVars;
set NewData;

NewVar1 = Col2 - Col1;
NewVar2 = Col3 - Col2;
Newvar3 = Col4 - Col3;

run;

An alternative approach without arrays.

/*-- Data from simonn's answer --*/
data SO1019005;
  input person var;
  datalines;
1 5
1 10
1 12
1 20
2 1
2 3
2 5
2 90
;
run;

/*-- Why not just do a transpose? --*/
proc transpose data=SO1019005 out=NewData;
by person;
run;

/*-- Now calculate your new vars --*/
data NewDataWithVars;
set NewData;

NewVar1 = Col2 - Col1;
NewVar2 = Col3 - Col2;
Newvar3 = Col4 - Col3;

run;
掀纱窥君容 2024-07-31 07:31:19

为什么不使用 diff() 函数呢?

/* test data */
data one;
  do id = 1 to 2;
    do v = 1 to 4 by 1;
      output;
    end;
  end;
run;
/* check */
proc print data=one;
run;
/* on lst
Obs    id    v

 1      1    1
 2      1    2
 3      1    3
 4      1    4
 5      2    1
 6      2    2
 7      2    3
 8      2    4
*/

/* now create diff within id */
data two;
  set one;
  by id notsorted; /* assuming already in order */
  dif = ifn(first.id, ., dif(v));
run;
proc print data=two;
run;
/* on lst
Obs    id    v    dif
 1      1    1     .
 2      1    2     1
 3      1    3     1
 4      1    4     1
 5      2    1     .
 6      2    2     1
 7      2    3     1
 8      2    4     1
*/

Why not use the dif() function instead?

/* test data */
data one;
  do id = 1 to 2;
    do v = 1 to 4 by 1;
      output;
    end;
  end;
run;
/* check */
proc print data=one;
run;
/* on lst
Obs    id    v

 1      1    1
 2      1    2
 3      1    3
 4      1    4
 5      2    1
 6      2    2
 7      2    3
 8      2    4
*/

/* now create diff within id */
data two;
  set one;
  by id notsorted; /* assuming already in order */
  dif = ifn(first.id, ., dif(v));
run;
proc print data=two;
run;
/* on lst
Obs    id    v    dif
 1      1    1     .
 2      1    2     1
 3      1    3     1
 4      1    4     1
 5      2    1     .
 6      2    2     1
 7      2    3     1
 8      2    4     1
*/
千柳 2024-07-31 07:31:19
data output_data;   
   retain count previous_value diff1 diff2 diff3;  
   set data input_data    
     by person;  
   if first.person then do;   
     count = 0;  
   end;  
   else do;  
     count = count + 1;  
     if count = 1 then diff1 = abs(value - previous_value);  
     if count = 2 then diff2 = abs(value - previous_value);  
     if count = 3 then do;   
       diff3 = abs(value - previous_value);   
       output output_data;   
     end;  
   end;  
   previous_value = value;  
run;
data output_data;   
   retain count previous_value diff1 diff2 diff3;  
   set data input_data    
     by person;  
   if first.person then do;   
     count = 0;  
   end;  
   else do;  
     count = count + 1;  
     if count = 1 then diff1 = abs(value - previous_value);  
     if count = 2 then diff2 = abs(value - previous_value);  
     if count = 3 then do;   
       diff3 = abs(value - previous_value);   
       output output_data;   
     end;  
   end;  
   previous_value = value;  
run;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文