SAS中文论坛

标题: 怎么理解 'if _n_=1 then set abc;' [打印本页]

作者: shiyiming    时间: 2013-12-17 05:25
标题: 怎么理解 'if _n_=1 then set abc;'
最近看到一个code:
data a;
  if _n_=1 then set b;   /*or if 0 then data b;*/
. . .
run;
怎么理解这个condition statement?
等同于data a; set b; run;?

thx!!!
作者: shiyiming    时间: 2013-12-27 02:39
标题: Re: 怎么理解 'if _n_=1 then set abc;'
just mean only at the first iteration (where the automatic variable _n_ is 1), copy variable (and value) from data set abc.  Note the value will be retained.

Compare with the following:
If 0 then set abc;             /*only copy variables, but no value*/
作者: shiyiming    时间: 2014-1-13 00:52
标题: Re: 怎么理解 'if _n_=1 then set abc;'
got you, thanks a lot!
作者: sunnyswing    时间: 2014-4-10 09:42
shiyiming 发表于 2013-12-27 02:39
just mean only at the first iteration (where the automatic variable _n_ is 1), copy variable (and va ...

I have the same question. But just look at here " Note the value will be retained.".How to maintain the value is not retained, as I just want one record from data set "abc"?
作者: davyin    时间: 2014-4-18 10:42
  GOT IT
作者: gogotiger    时间: 2014-5-13 05:08
I got duplicates when I tried the following:
data a; if _n_=1 then set sashelp.class;  run;

anyone can help? Thx!!!!
作者: bird    时间: 2014-8-20 09:21
gogotiger 发表于 2014-5-13 05:08
I got duplicates when I tried the following:
data a; if _n_=1 then set sashelp.class;  run;

data a;
  if _n_ = 1 then set abc;
  else stop;
run;

作者: gogotiger    时间: 2014-9-6 22:45
thxs bird!
作者: berry_li    时间: 2014-11-27 22:04
bird好像是我吧。。。我了个去。。。




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2