在 SAS 中创建汇总变量的简单方法

发布于 2024-07-23 05:06:44 字数 91 浏览 6 评论 0原文

我正在寻找一种在 SAS 中创建汇总变量的方法,该变量将自动添加每行观察值,直到满足条件。 如有必要,我需要能够启动、停止和重置此变量。

多谢。

I am looking for an way to create a summary variable in SAS that will automatically add each row observation until a condition is met. I would need to be able to start and stop and reset this variable if necessary.

Thanks a lot.

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

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

发布评论

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

评论(2

壹場煙雨 2024-07-30 05:06:44

使用保留!

data test;
set test;
retain VarSummary;
VarSummary+YourVar;
if condition then VarSummary=SummatElse;
run;

希望这是有道理的!

Use Retain!

data test;
set test;
retain VarSummary;
VarSummary+YourVar;
if condition then VarSummary=SummatElse;
run;

Hope this makes sense!

棒棒糖 2024-07-30 05:06:44

如果您要使用

VarSummary + 1  ; 

OR

VarSummary + <expression> ;

之类的语句,那么您实际上不需要 RETAIN 语句。

另外,如果您在 DATA STEP 中使用 BY 语句,则可以访问变量 FIRST 和 LAST(数据必须按 BY 变量排序)。 FIRST 和 LAST 的值为 1 或 0。当 BY 变量位于第一个值 FIRST 时。 = 1 并且当它位于最后一个值 LAST 时。 = 1。当只有 1 条 byVariable 记录时,它们都可以等于 1;当 byVariable 记录多于 2 条时(当在中间记录时),它们都可以等于 0。

FIRST 和 LAST 可以帮助确定何时重置 RETAINed 变量。 (FIRST和LAST将在PDV中,但不会写入输出数据集,因此不需要DROP它们)。

示例

(此示例的结果可能是通过 PROC 完成的,但我希望这演示了如何使用 FIRST 和 LAST)

proc sort data=sasuser.laguardia out=work.dest;
 by dest ;
run ;

data work.destination_summary (keep=dest dest_count total_count) ;
 set work.dest ;
 by dest ;

 total_count + 1 ;

 if first.dest then dest_count = 1 ;
 if not first.dest and not last.dest then dest_count + 1 ;
 if last.dest then do ;
  dest_count + 1 ;
  output ;
 end ;
run ;

proc print data=work.destination_summary label noobs ;
 var dest dest_count total_count;
 label Dest="Destination"
   dest_count="Count" 
   total_count= "Total Count";
run ;

If you are going to use a statement like

VarSummary + 1  ; 

OR

VarSummary + <expression> ;

then you don't actually need a RETAIN statement.

Also, if you use the BY statement in your DATA STEP, you have access to variables FIRST and LAST (The data must be sorted by the BY variable). FIRST and LAST either have a value of 1 or 0. When the BY variable is on the first value FIRST. = 1 and when it is on the last value LAST. = 1. They can both be equal to 1 when there is only 1 byVariable record and they can both be equal to 0 when there is more than 2 byVariable records (when on the middle records).

FIRST and LAST can help determine when to reset your RETAINed variables. (FIRST and LAST will be in the PDV, but will not be written to the output data set, so there is no need to DROP them).

Example

(The result of this example would probably be done with a PROC, but I hope this demonstrates how FIRST and LAST can be used)

proc sort data=sasuser.laguardia out=work.dest;
 by dest ;
run ;

data work.destination_summary (keep=dest dest_count total_count) ;
 set work.dest ;
 by dest ;

 total_count + 1 ;

 if first.dest then dest_count = 1 ;
 if not first.dest and not last.dest then dest_count + 1 ;
 if last.dest then do ;
  dest_count + 1 ;
  output ;
 end ;
run ;

proc print data=work.destination_summary label noobs ;
 var dest dest_count total_count;
 label Dest="Destination"
   dest_count="Count" 
   total_count= "Total Count";
run ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文