SAS:如何随机提取X年

发布于 2025-02-02 16:16:19 字数 349 浏览 1 评论 0原文

因此,有一个数据集包含Y年数据,我的问题是如何随机选择X年以上的样本。 例如;有2001年至2005年的观察结果

表明,我想在总计5年的数据中随机两年吗?例如(2001年和2003年; 2001和2002;等) 我该如何在数据步骤中而不是使用PROC调查选择中执行此操作,因为我还没有达到这一点。如果在数据步骤中无法完成如何解决?

data have;
    input fyear data;
    datalines;
2001 123
2002 123
2001 123
2003 123
2001 123
2003 123
2004 123
2005 123
2001 123
2003 123
;
run;

So, There is a dataset contains Y years data in total, My question is how to select a sample that has X years out of Y years randomly.
For instance; there is 2001 to 2005 years observations

say I want two random years out of a total of 5 years data? For example (2001 and 2003; 2001 and 2002;etc)
How can i do this in a DATA step instead of using PROC SURVEY SELECTION, since I haven't reach that. If it cant be done in DATA step how to solve it?

data have;
    input fyear data;
    datalines;
2001 123
2002 123
2001 123
2003 123
2001 123
2003 123
2004 123
2005 123
2001 123
2003 123
;
run;

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

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

发布评论

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

评论(1

塔塔猫 2025-02-09 16:16:19

创建一个宏变量,该变量在数据集中拥有所有年份的随机列表。列表中的前两个值将是随机的有效年份。

proc sql noprint outobs=2;
    select fyear
    into :years separated by ','
    from (select distinct fyear from have)
    order by rand('uniform')
    ;
quit;

查看宏变量& YARS

%put &years;

2001,2004

我们可以在语句中的中直接使用它:

data sample;
    set have;
    where fyear IN (&years);
run;

更改Proc SQL中OutObs的值增加或减少随机年数。

Create a macro variable that holds a distinct randomized list of all years in your dataset. The first two values in the list will be random valid years.

proc sql noprint outobs=2;
    select fyear
    into :years separated by ','
    from (select distinct fyear from have)
    order by rand('uniform')
    ;
quit;

Take a look at the macro variable &years:

%put &years;

2001,2004

We can use this directly in an in statement:

data sample;
    set have;
    where fyear IN (&years);
run;

Change the value of outobs in PROC SQL to increase or decrease the number of random years.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文