楼主: lovemanbbs
20745 29

[原创博文] _N_是什么意思? [推广有奖]

  • 0关注
  • 0粉丝

已卖:210份资源

博士生

69%

还不是VIP/贵宾

-

威望
0
论坛币
566 个
通用积分
0.5400
学术水平
-5 点
热心指数
1 点
信用等级
-5 点
经验
4041 点
帖子
193
精华
0
在线时间
388 小时
注册时间
2006-3-6
最后登录
2020-12-17

楼主
lovemanbbs 发表于 2010-8-5 10:03:37 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Example Here are the flower sales data consisting of customer ID, date of sale, variety of flower, and quantity.
240W 02-07-2008 Ginger 120
240W 02-07-2008 Protea 180
356W 02-08-2008 Heliconia 60
356W 02-08-2008 Anthurium 300
188R 02-11-2008 Ginger 24
188R 02-11-2008 Anthurium 24
240W 02-12-2008 Heliconia 48
240W 02-12-2008 Protea 48
356W 02-12-2008 Ginger 240
In this example, the grower wants a program that will find the customer with the single largest order, and print all the orders for that customer.
* Read the raw data;
DATA flowersales;
INFILE 'c:\MySASLib\TropicalSales.dat';
INPUT CustomerID $4. @6 SaleDate MMDDYY10. @17 Variety $9. Quantity;
PROC SORT DATA = flowersales;
BY DESCENDING Quantity;
RUN;
* Find biggest order and pass the customer id to a macro variable;
DATA _NULL_;
SET flowersales;
IF _N_ = 1 THEN CALL SYMPUT("selectedcustomer",CustomerID);
ELSE STOP;
RUN;
PROC PRINT DATA = flowersales;
WHERE CustomerID = "&selectedcustomer";
FORMAT SaleDate WORDDATE18.;
TITLE "Customer &selectedcustomer Had the Single Largest Order";
RUN;

请问各位高手,IF _N_ = 1 是什么意思?
网上查到是:An internal SAS counter in the data step which contains the current iteration number of the data step.
能不能具体点?好像还没理解
谢谢大虾
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:是什么意思 Data step iteration Customer Selected customer example single

回帖推荐

moonstarpursuit 发表于2楼  查看完整内容

_n_是SAS系统变量,是在运行过程中产生的变量,作用就是记录当前是第几条record在运行

本帖被以下文库推荐

沙发
moonstarpursuit 发表于 2010-8-5 10:14:19
_n_是SAS系统变量,是在运行过程中产生的变量,作用就是记录当前是第几条record在运行
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
rambo419 + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

藤椅
BraveMadMan 发表于 2010-8-5 10:16:27
the first observation in the dataset.
Don't get lost in technical details. What is the big picture?

板凳
ststar 发表于 2010-8-5 10:20:01
与STATA中的_n,_N类似

报纸
honghejing 发表于 2010-8-5 10:32:38
_n_=1就是第一行。。。

地板
soporaeternus 发表于 2010-8-5 10:42:11
data步的循环次数
某种意义上是第几条记录......
Let them be hard, but never unjust

7
lovemanbbs 发表于 2010-8-5 10:55:29
6# soporaeternus
如果循环1次的话,应该输出
Customer 356W Had the Single Largest Order
       Customer
Obs    ID               SaleDate             Variety    Quantity
1      356W      February 8, 2008     Anthurium    300

而书上是:
Customer 356W Had the Single Largest Order
       Customer
Obs    ID               SaleDate             Variety    Quantity
1      356W      February 8, 2008     Anthurium    300
2      356W      February 12, 2008   Ginger         240
5      356W      February 8, 2008     Heliconia       60
是我哪里弄错了吗?麻烦能不能回答一下?
已有 1 人评分论坛币 收起 理由
nlm0402 + 5 精彩帖子

总评分: 论坛币 + 5   查看全部评分

8
moonstarpursuit 发表于 2010-8-5 11:06:14
没错吧,它只是把ID记住了。根据where语句的定义,它是找出ID,然后输出这个ID的所有记录

9
pobel 在职认证  发表于 2010-8-5 11:15:46
* Find biggest order and pass the customer id to a macro variable;
DATA _NULL_;
SET flowersales;
IF _N_ = 1 THEN CALL SYMPUT("selectedcustomer",CustomerID);这里找出了order最大的customerid;
ELSE STOP;
RUN;



PROC PRINT DATA = flowersales;
WHERE CustomerID = "&selectedcustomer"; 这里选出该ID的所有记录
FORMAT SaleDate WORDDATE18.;
TITLE "Customer &selectedcustomer Had the Single Largest Order";
RUN;
和谐拯救危机

10
moonstarpursuit 发表于 2010-8-5 11:35:52
问题的要求是要“find the customer with the single largest order, and print all the orders for that customer”,楼主运行结果与书上不一致的主要问题在于:
INPUT CustomerID $4. @6 SaleDate MMDDYY10. @17 Variety $9. Quantity;
上面的输入格式是mixed!
楼主可以试着把“Variety $9. ”中“$”符号前面加“:”,变成“Variety : $9.”再试试,输出的结果应该就一致了!
已有 1 人评分论坛币 收起 理由
nlm0402 + 5 精彩帖子

总评分: 论坛币 + 5   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 05:39