楼主: sun5008
4591 9

[原创博文] data步的简单循环问题,谢谢 [推广有奖]

  • 0关注
  • 0粉丝

已卖:993份资源

博士生

45%

还不是VIP/贵宾

-

威望
0
论坛币
96 个
通用积分
2.1000
学术水平
0 点
热心指数
2 点
信用等级
0 点
经验
6239 点
帖子
140
精华
0
在线时间
270 小时
注册时间
2006-3-9
最后登录
2025-11-25

楼主
sun5008 发表于 2011-9-13 23:16:11 |AI写论文
50论坛币
data yy;
input x y;
cards;
1 0
2 0.4
3 1.1
4 1.3
5 1.4
6 1.1
7 0.9
8 1.1
9 0.4
10 0.8
;run;
元数据如上: 当y大于1时,买入;一旦买入后,持有至Y小于0.5或者大于1.5才卖出,
我想在set数据集的时候,再加一列c,作为判断,比如“Buy”或“sell”

请高手帮我解决,要求是在data步内的循环,如:
data xx;
do x=1 to 202;
set yy;
if Y>1 then do;c="buy";output;end;
.....

我想到的是结合goto语句,但是做恶劣半天都不行~

关键词:data步 Data 单循环 output Input

回帖推荐

caibirdcnb 发表于10楼  查看完整内容

本帖被以下文库推荐

沙发
lihun 发表于 2011-9-13 23:42:52
我也看不懂...

藤椅
zhangzachary 发表于 2011-9-13 23:52:57
  1. data yy;
  2. input x y;
  3. cards;
  4. 1 0
  5. 2 0.4
  6. 3 1.1
  7. 4 1.3
  8. 5 1.4
  9. 6 1.1
  10. 7 0.9
  11. 8 1.1
  12. 9 0.4
  13. 10 0.8
  14. ;run;


  15. data xx;
  16. length c $4;
  17. set yy;
  18. retain s 0;
  19. if s=0 then do;
  20.   if y>1 then do;
  21.   c="buy";
  22.   s=1;
  23.   end;
  24.   else c="no";
  25. end;
  26. else do;
  27.   if y<0.5 or y>1.5 then do;
  28.   c="sell";
  29.   s=0;
  30.   end;
  31.   else c="yes";
  32. end;
  33. drop s;
  34. run;
复制代码
c="buy" means we buy now
c="sell" means we sell now
c="yes" means we have it
c="no" means we dont have it
寒冰凤凰 My blog: http://blog.sina.com.cn/u/1058955485

板凳
xiaoyu163 发表于 2011-9-14 04:36:36
向ls大牛学习,对循环语句和retain的用法有了更深的认识

报纸
sun5008 发表于 2011-9-14 08:49:24
zhangzachary 发表于 2011-9-13 23:52
c="buy" means we buy now
c="sell" means we sell now
c="yes" means we have it
你好,你的语句可以实现目前的功能,但是你所有的IF 和 Else If都处在一个数据行上,
我其实后面的判断条件还有好几个,如果写很多个IF Else的话不现实,而且If的条件有时还要用到上一行的状态

,我想实现的是:
根据第一行判断,if。。。then。。。,如果合适了跳至下一行,所有想用goto解决
如:

data xx;
set yy;
if Y>1 then do;
                      c="买";goto jj;output;
                 end;
jj: if (y>1.5 or y<0.5) then  do;
                     c="平仓";end;
         else c="持有";                    
.....
我的意思是,这样一行一行的判断下去,。。。但是对SAS的DATA步里面的Goto,循环还不是很清楚,请高手指点~~

地板
zhangzachary 发表于 2011-9-14 15:31:45
sun5008 发表于 2011-9-14 08:49
你好,你的语句可以实现目前的功能,但是你所有的IF 和 Else If都处在一个数据行上,
我其实后面的判断 ...
其实程序可以优化的......
比如认为if else比较麻烦,可以用if then do然后return end;这样就可以分别对各种条件进行判别:
  1. data xx;
  2. length c $4;
  3. set yy;
  4. retain s 0;
  5. if s=0 then do;
  6.   if y>1 then do;
  7.   c="buy";
  8.   s=1;
  9.   end;
  10.   else c="no";
  11.   return;
  12. end;
  13. if s=1 then do;
  14.   if y<0.5 or y>1.5 then do;
  15.   c="sell";
  16.   s=0;
  17.   end;
  18.   else c="yes";
  19.   return;
  20. end;
  21. drop s;
  22. run;
复制代码
寒冰凤凰 My blog: http://blog.sina.com.cn/u/1058955485

7
zhangzachary 发表于 2011-9-14 15:34:24
sun5008 发表于 2011-9-14 08:49
你好,你的语句可以实现目前的功能,但是你所有的IF 和 Else If都处在一个数据行上,
我其实后面的判断 ...
一般程序都尽量避免goto语句的,而且像这类的程序也不需要用goto... 当然这得需要大牛来帮忙,一般用得到goto的都不是一般人
寒冰凤凰 My blog: http://blog.sina.com.cn/u/1058955485

8
soporaeternus 发表于 2011-9-14 15:59:42
要不提供下更多的细节吧,就这点要求的话楼上的代码很好啊
Let them be hard, but never unjust

9
zkymath 在职认证  发表于 2011-9-14 18:04:45
goto 不是 必须的

10
caibirdcnb 发表于 2011-9-14 19:38:34
  1. data yy;
  2. input x y;
  3. cards;
  4. 1 0
  5. 2 0.4
  6. 3 1.1
  7. 4 1.3
  8. 5 1.6
  9. 6 1.1
  10. 7 0.9
  11. 8 1.1
  12. 9 0.4
  13. 10 0.8
  14. ;run;

  15. data xx;
  16. length c $4;
  17. set yy;
  18. *没买或者之前已经卖掉的股票,如果涨幅大于1,则买入;
  19. if c = '' or c = 'sell' then
  20.         do;
  21.                 if y > 1 then c = 'buy';
  22.         end;
  23. *买入的股票,根据设定卖出;
  24. else if c = 'buy' then
  25.         do;
  26.                 if y < 0.5 or y > 1.5 then c = 'sell';
  27.         end;
  28. run;
复制代码

已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-3 03:47