SAS:可以返回 SAS 数据步骤中的先前观察结果吗?

发布于 2024-11-06 13:25:57 字数 1349 浏览 4 评论 0原文

我也在 RunSubmit 上问了这个问题,因为 SAS 问答社区似乎有点分散。如果不理解这一点,请告诉我。

是否可以在数据步骤中返回到先前/特定的观察并从那里进一步浏览数据集?

为了防止问题仍然不清楚,添加一个小例子:

数据集 'work.test' :

name  |   number   
John  |    1      
Jack  |    2  
Jane  |    3     
Jade  |    4       
Ronn  |    5       
Dick  |    6       
Sofy  |    7      
Sady  |    8      
Ruth  |    9      

数据步骤:

Data _null_;
 set work.test;
 File ...\test.txt;
 put name;
 if number = 5 and counter=3 then do;
   counter = counter+1;
   *return to obs where number = 3* AKA *set obs pointer back to obs with nr=3*;
 end;
run;

*问题是关于 return to obs where number = 1 的部分,不关于 [和第一次],只是添加它,这样它就不会生成无限循环。*

附加信息:
由于似乎仍然不是 100% 清楚我想要做什么,我在原始示例数据集和示例中添加了一些内容。 请记住,它应该有点通用,而不是固定代码。稍后情况可能会有所不同。但主要问题只是:“当您位于 obs=Y 时是否可以返回 obs=X 并从那里继续,如何进行?”

背景信息: 这符合使用包含 xml 流的单个表创建 xml 输出的整个过程,其中一些元素需要重复。不,由于 SAS 版本较旧,无法使用 XML 映射。不,ODS 也不适用于这种情况。顺便说一句,这只是背景信息,因为不断出现“我仍然不知道你想做什么”的评论;)

不适合我的需求的可能性列表:

  • REWIND 函数(仅返回第一个观察)
  • LAG 函数(获取变量的先前值,而不是返回 obs)
  • 使用多个 SET 语句:这不是我需要的通用方式(即某些数据集需要 2 个循环,其他数据集需要 5 个循环,因此 5 个嵌套 SET语句...)

什么可以工作:

  • 使用完整的SCL代码来完成数据步骤(推迟)
  • SET语句中的POINT选项(当前尝试)
  • 哈希表:仍然需要做更多研究,因为我对此不太了解或如何实施。

I asked this question on RunSubmit too, since SAS Q&A community seems a bit scattered. If this is not appreciated, please let me know.

Is it possible in a data step to return to a previous/certain observation and go from there further through the data set?

To add to the question in case it is still not clear, a small example:

Data set 'work.test' :

name  |   number   
John  |    1      
Jack  |    2  
Jane  |    3     
Jade  |    4       
Ronn  |    5       
Dick  |    6       
Sofy  |    7      
Sady  |    8      
Ruth  |    9      

Data step:

Data _null_;
 set work.test;
 File ...\test.txt;
 put name;
 if number = 5 and counter=3 then do;
   counter = counter+1;
   *return to obs where number = 3* AKA *set obs pointer back to obs with nr=3*;
 end;
run;

*The question is about the part return to obs where number = 1 , not about
[and first time] , that is only added so it would not generate and endless loop.*

Additional info:
As it seems to still not be 100% clear what I want to do, I added a bit to the original sample data set and example.
Just remember it should be a bit generic and not be fixed code. Conditions might vary later on. But the main question is just: "is it possible to go back to obs=X and go from there when you are at obs=Y and how?"

Background info:
This fits into the whole story of creating an xml output using a single table holding the xml flow, where some elements need to be repeated. No, it is not possible using XML map because of old SAS version. No, ODS is also not applicable in this case. Btw, this is just background info because the remark 'I still don't knwo what you try to do' keeps coming up ;)

List of possibilities NOT applicable for my needs:

  • REWIND function (returns only to the first observation)
  • LAG function (to get previous value of variable, not for going back in obs)
  • Using multiple SET statements: this is not generic in the way I need it (ie. some data sets would require 2 loops, others would require 5 loops, thus 5 nested SET statements...)

What COULD work:

  • Use of complete SCL code to go through data step (postponed)
  • POINT option in the SET statement (current try out)
  • Hash tables: still have to do more research as I do not know much about this or how to implement.

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

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

发布评论

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

评论(2

相守太难 2024-11-13 13:25:57

SET 语句是可执行类型。

/* test data */
data one;
  input name $ num;
cards;
John 1 
Jack 2 
Jane 3 
Jade 4
;
run;

/* output obs until num=3 then output all */
data two;
   do until (num=3 or end1);
      set one end=end1;
      output;
   end;
   do until (end2);
      set one end=end2;
      output;
   end;
   stop;
run;

/* check */
proc print data=two;
run;
/* on lst
Obs    num    name

 1      1     John
 2      2     Jack
 3      3     Jane
 4      1     John
 5      2     Jack
 6      3     Jane
 7      4     Jade
*/

The SET statement is of an executable type.

/* test data */
data one;
  input name $ num;
cards;
John 1 
Jack 2 
Jane 3 
Jade 4
;
run;

/* output obs until num=3 then output all */
data two;
   do until (num=3 or end1);
      set one end=end1;
      output;
   end;
   do until (end2);
      set one end=end2;
      output;
   end;
   stop;
run;

/* check */
proc print data=two;
run;
/* on lst
Obs    num    name

 1      1     John
 2      2     Jack
 3      3     Jane
 4      1     John
 5      2     Jack
 6      3     Jane
 7      4     Jade
*/
梦里人 2024-11-13 13:25:57

SAS 数据步骤主要一次对一个观察进行操作,因此我相信 sasfrog 关于哈希对象的评论可能是您最好的选择。以下代码也可能有帮助(两个 SET 语句);但我仍然不确定你的最终预期输出是什么,所以这只是一个粗鲁的例子。

data test1;
   set test;
   if number eq 5 then do;
      set test(rename=(name=name2 number=counter) where=(counter=3));
   end;
run;

The SAS data step primarily operates on one observation at a time, so I believe that sasfrog's comment regarding hash objects might be your best bet. The following code might also be of help (two SET statements); but I'm still not sure what your final expected output is, so this is just a rude example.

data test1;
   set test;
   if number eq 5 then do;
      set test(rename=(name=name2 number=counter) where=(counter=3));
   end;
run;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文