楼主: Tigflanker
2883 6

询问一个嵌套循环修改约束条件的问题 [推广有奖]

  • 8关注
  • 18粉丝

副教授

49%

还不是VIP/贵宾

-

威望
0
论坛币
2321 个
通用积分
9.9128
学术水平
179 点
热心指数
194 点
信用等级
167 点
经验
27443 点
帖子
622
精华
0
在线时间
851 小时
注册时间
2011-3-27
最后登录
2023-5-14

楼主
Tigflanker 发表于 2013-3-15 09:29:56 |AI写论文
200论坛币
代码:

%macro search(w0by,delby,sigby,sig1,sig2,need);

data ;
  do w0 =0 to 80 by &w0by;
    do delta10 =15 to 30 by &delby;
re:   do sigma10 =&sig1 to &sig2 by &sigby;
*循环体;
                    if abs(nc)<&need then do;
/*                        put w0= delta10= sigma10= nc=;*/
                        leave;                *本段用于结束最内层循环;                           
                        end;
                       
                    if nc<0 then do;
                        put w0= delta10= sigma10= nc=;
                        call symputx("sig1",sigma10-&sigby);
                        call symputx("sig2",sigma10);
            call symputx("sigby",&need);
                        go to re;   *本段试图修改内层循环的约束条件,并重做内层循环;
                        end;
      end;
    end;
  end;
run;

%mend;

%search(10,5,10,50,150,0.1);


问各位老师一个问题,代码如上,描述如下:

我想做最内层的循环迭代,从而得出最优结果,我试图利用宏变量去修改最内层循环的约束条件,但是失败了,似乎do循环不允许这样修改;请问我的这种思路可行吗?

另外,我干脆把改成宏do循环如下,但是中间有个squt要出问题:

%macro search(w0by,delby,sigby,sig1,sig2,need);

%let w0=0;%let delta10=15;%let sigma10=&sig1;

%do %until(&w0=80);
  %do %until(&delta10=30);
%re:%do %until(&sigma10=&sig2);
      data a;
      w=&w0/100;wp=1-w;delta1=&delta10/100;sigma1=&sigma10/100;
*%do循环体;  *循环体中的sqrt方法报错,方法中的所有值都是未缺失的,方法得出的结果缺失;

call  symputx("w0",w0);   *这几句试图提取关键变量结果至宏变量;
call  symputx("delta10",delta10);
call  symputx("sigma10",sigma10);
call  symputx("nc",nc);%put %sysfunc(abs(%sysevalf(&nc)));

                %if %sysfunc(abs(%sysevalf(&nc)))<&need %then %do;
                %put w0=&w0 delta10=&delta10 sigma10=&sigma10 nc=&nc;
                /*leave;*/
                %end;   *未做好的结束条件;
               
                %if &nc<0 %then %do;
                %put w0=&w0 delta10=&delta10 sigma10=&sigma10 nc=&nc;
                %let sig1=%sysevalf(&sigma10-&sigby);
                %let sig2=&sigma10;
        %let sigby=&need;
                %goto re;   *本段试图修改%do的约束条件;
                %end;
      %let sigma10=%sysevalf(&sigma10+&sigby);%end;
    %let delta10=%sysevalf(&delta10+&delby);%end;
  %let w0=%sysevalf(&w0+&w0by);%end;
run;

%mend;

%search(10,5,10,50,150,0.1);


请求帮助,谢谢啊。

最佳答案

yongyitian 查看完整内容

1. You can add a few put statement to see where the loop goes. 2. add a _null_ after the data if you don't want output to a dataset. 3. add a nc value to try the loop when nc = 0.05, you can see that the second if condision is not reached but when nc=-0.2(which abs(nc) > need ) the second become non-stop because the go to statement. Be carefull about this. %macro search(w0by,delby, ...
关键词:约束条件 search Macro Sigma Delta search
Bye SAS.
若有缘,能重聚。

沙发
yongyitian 发表于 2013-3-15 09:29:57
1. You can add a few put statement to see where the loop goes.
2. add a _null_ after the data if you don't want output to a dataset.
3. add a nc value to try the loop
   when nc = 0.05, you can see that the second if condision is not reached
   but when nc=-0.2(which abs(nc) > need ) the second become non-stop because the go to statement. Be carefull about this.


%macro search(w0by,delby,sigby,sig1,sig2,need);

data _null_;
  do w0 =0 to 80 by &w0by;
    do delta10 =15 to 30 by &delby;
re:   do sigma10 =&sig1 to &sig2 by &sigby;
/* loop */     nc=0.05;
put 'Position A '  _all_  ;   
                    if abs(nc)<&need then do;
put 'Position B ' _all_;   
/*                        put w0= delta10= sigma10= nc=;*/
                        leave;         /* this part is for inner loop */  
put 'position C ' _all_;
                        end;
put 'Position D ' _all_;                        
                    if nc<0 then do;
put 'Position E ' _all_;
                        put w0= delta10= sigma10= nc=;
                        call symputx("sig1",sigma10-&sigby);
                        call symputx("sig2",sigma10);
            call symputx("sigby",&need);
                        go to re;    /* this is for inner loop constraint */
                        end;
      end;
    end;
  end;
run;

%mend;

%search(10,5,10,50,150,0.1);

藤椅
Imasasor 发表于 2013-3-15 09:34:48
太长了,不想看
欢迎加入亚太地区第一R&Python数据挖掘群: 251548215;

板凳
yongyitian 发表于 2013-3-15 09:51:06
看一看nc 的值是什么?
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Tigflanker + 1 + 1 + 1 是循环体中一大堆算得的最终结果。

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

报纸
playmore 发表于 2013-3-15 13:19:45
你的意思是用%do循环中得到的结果修改之前%do循环的循环条件
在SAS中,%do循环应该在编译的时候就处理好了,所以你之前的做法是无效的

还有你那个nc值不是事先给定的嘛,不能在循环之前就计算好每步的循环次数吗?

还有你必须要用穷举做最优化吗?想想其他的办法也好
playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

地板
Tigflanker 发表于 2013-3-17 15:13:19
playmore 发表于 2013-3-15 13:19
你的意思是用%do循环中得到的结果修改之前%do循环的循环条件
在SAS中,%do循环应该在编译的时候就处理好了 ...
感谢,您的答复很正确,我现在将最内层的Do To循环改成了Do Until循环,希望能够解决。。
Bye SAS.
若有缘,能重聚。

7
Tigflanker 发表于 2013-3-17 15:14:49
yongyitian 发表于 2013-3-15 10:56
1. You can add a few put statement to see where the loop goes.
2. add a _null_ after the data if yo ...
感谢,怪我没交代清楚,nc是一系列计算产生的值,从1000单调降至-10,欲寻求0时某参数的估计值。
Bye SAS.
若有缘,能重聚。

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

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