楼主: silentzilch
2010 10

[有偿编程] 悬赏500论坛币求 SAS MACRO 高手! 第二部分! [推广有奖]

  • 1关注
  • 0粉丝

大专生

5%

还不是VIP/贵宾

-

威望
0
论坛币
143 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
396 点
帖子
15
精华
0
在线时间
64 小时
注册时间
2015-1-15
最后登录
2023-3-6

500论坛币

最佳答案

pobel 查看完整内容

%macro test(n); data test; %do i=1 %to &n; do v&i =1 to &n; %if &i=1 %then %do; tmp&i=cats("*",v&i,"*"); %end; %else %do; if index(tmp%eval(&i-1), cats("*",v&i,"*")) then continue; else tmp&i="*"||catx("*",of v1-v&i)||"*"; %end; %if &i=&n %then output;; %end; %do i=1 %to &n; end; %end; drop tmp:; ...
关键词:500论坛币 悬赏500 Macro 0论坛币 Mac 管理员
沙发
pobel 在职认证  发表于 2015-4-9 10:40:07 |只看作者 |坛友微信交流群
%macro test(n);
    data test;
            %do i=1 %to &n;
                    do v&i =1 to &n;
                    %if &i=1 %then %do;
                           tmp&i=cats("*",v&i,"*");
                        %end;
                        %else %do;
                            if index(tmp%eval(&i-1), cats("*",v&i,"*")) then continue;
                                else tmp&i="*"||catx("*",of v1-v&i)||"*";
                        %end;
                        %if &i=&n %then output;;
                 %end;
                 %do i=1 %to &n;
                end;
          %end;
                  drop tmp:;
        run;
%mend;

%test(4)
%test(6)

使用道具

藤椅
yongyitian 发表于 2015-4-9 20:00:28 |只看作者 |坛友微信交流群
  1. * Note: call allperm 最多可以对18个变量进行排列. Don't try it!;

  2. %let n=4;
  3. data PermFact;
  4.    array x[&n] (1:&n);
  5.      nfact=fact(&n);
  6.       do i=1 to nfact;
  7.       call allperm(i, of x[*]);
  8.      output;
  9.     end;
  10.         keep x1-x&n;
  11. run;
  12. proc sort data=PermFact;
  13.   by x1-x&n;
  14. run;
复制代码
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
Tigflanker + 5 + 3 + 3 + 3 观点有启发

总评分: 论坛币 + 5  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

使用道具

板凳
jl60156 发表于 2015-4-10 03:12:42 |只看作者 |坛友微信交流群
%macro perm(r) / parmbuff;   
      %let i=2;               
      %let things=;                     
      %do %while (%scan(&syspbuff,&i) ne );     
        %let p&i="%scan(&syspbuff,&i)";         
        %if &i=2 %then %let things=&&p&i;   
        %else %let things=&things,&&p&i;        
        %let i=%eval(&i+1);
      %end;
      %let n=%eval(&i-2);
      data perm;
         drop i j copy;
         array check (*) $ 10 r1-r&r;        
          %do m=1 %to &r;                 
            do r&m = &things;
          %end;
          copy=0;
            do i=2 to &r;               
              do j=1 to i-1;            
                if check(j)=check(i) then copy+1;
              end;
            end;
          if copy = 0 then output;        
          if copy = 0 then put r1-r&r;      
          %do m=1 %to &r;
            end;                             
          %end;
        run;
   %mend perm;
%perm(5,1,2,3,4,5);

使用道具

报纸
silentzilch 发表于 2015-4-10 10:41:55 |只看作者 |坛友微信交流群
yongyitian 发表于 2015-4-9 20:00
感谢您的回答!看了您的代码查了一下才知道了allperm这个功能的存在。
不知道有没有什么办法可以不依靠allperm这个功能来实现。

使用道具

地板
silentzilch 发表于 2015-4-10 10:47:50 |只看作者 |坛友微信交流群
jl60156 发表于 2015-4-10 03:12
%macro perm(r) / parmbuff;   
      %let i=2;               
      %let things=;                  ...
感谢您的回答!有些地方看的一知半解。
最大的问题在于呼出宏时只能给出一个数值而非给定一串数据。

使用道具

7
yongyitian 发表于 2015-4-10 11:37:32 |只看作者 |坛友微信交流群
silentzilch 发表于 2015-4-10 10:41
感谢您的回答!看了您的代码查了一下才知道了allperm这个功能的存在。
不知道有没有什么办法可以不依靠a ...
楼上的 macro 不用 call allperm routine。
也可用于其他字符串, 并指定需要排列得字符串得个数. 例如 %permute(3,aaa, bbb, ccc,ddd)
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002208648.htm

http://www.urz.uni-heidelberg.de ... /ts498-combperm.txt

使用道具

8
silentzilch 发表于 2015-4-11 09:04:01 |只看作者 |坛友微信交流群
yongyitian 发表于 2015-4-10 11:37
楼上的 macro 不用 call allperm routine。
也可用于其他字符串, 并指定需要排列得字符串得个数. 例如  ...
感谢您的回答!

使用道具

9
silentzilch 发表于 2015-4-11 09:05:37 |只看作者 |坛友微信交流群
pobel 发表于 2015-4-10 13:27
%macro test(n);
    data test;
            %do i=1 %to &n;
感谢您的回答!

使用道具

10
sas9.4 发表于 2015-4-15 00:33:15 |只看作者 |坛友微信交流群
%let n=5  ;   /*n should <=18*/
proc iml;
  n = &n;
  p = allperm(n);
  print p;  /* this is the result*/
create jiecheng var{p};   
   append;
   show contents;
   close jiecheng;
run;

/* the following step is just for output dataset ;*/
data result;
  set jiecheng;
  array xxx[&n] a1-a&n;
  retain xxx;
  count+1;
  xxx[count]= p;
  if count=&n then do;
    call missing (count);
    output;
  end;
  keep a: ;
run;

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-4 19:31