楼主: edwardzxf
3721 10

关于字符串的排列组合 [推广有奖]

  • 0关注
  • 2粉丝

讲师

26%

还不是VIP/贵宾

-

威望
0
论坛币
222 个
通用积分
0.3603
学术水平
0 点
热心指数
2 点
信用等级
0 点
经验
3991 点
帖子
242
精华
0
在线时间
375 小时
注册时间
2010-7-30
最后登录
2021-5-25

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
假设宏变量  %let str=v1 v2 v3;       /*v的个数可能超过3,v1 v2 v3 v4.....vn*/
我要生成几个新的宏变量,每一个新宏变量的值是 v1 v2 v3组合的一种情况,如:
str1=v1
str2=v2
str3=v3
str4=v1 v2
str5=v1 v3
str6=v2 v3

请教下,哪位能有好的,快捷实现的方法,谢谢!!

二维码

扫码加我 拉你入群

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

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

关键词:字符串 let chuan 字符串

沙发
PhoenixHuang 发表于 2013-9-22 22:49:00 |只看作者 |坛友微信交流群
let me try:

data test;
  input serial var $;
  datalines;
  1 v1
  2 v2
  3 v3
  ;
  run;

proc sql;
    create table tmp as(
    select var as newvar from test union all
    select cat(a.var,b.var) as newvar from test a,test b where  a.serial<b.serial union all
    select cat(a.var,b.var,c.var) as newvar from test a,test b,test c where  a.serial<b.serial and b.serial<c.serial);
quit;

data fstr;
     set tmp;
     serial +1;
run;

proc sql;
   select newvar into :str1 from fstr where serial=1;
   select newvar into :str2 from fstr where serial=2;
   select newvar into :str3 from fstr where serial=3;
   select newvar into :str4 from fstr where serial=4;
   select newvar into :str5 from fstr where serial=5;
   select newvar into :str6 from fstr where serial=6;
quit;

%put &str1;
%put &str2;
%put &str3;
%put &str4;
%put &str5;
%put &str6;

使用道具

藤椅
邓贵大 发表于 2013-9-23 00:41:47 |只看作者 |坛友微信交流群
1. Functions allcomb, allperm
http://support.sas.com/documenta ... .htm#a003112302.htm
2. PROC PLAN
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

使用道具

板凳
edwardzxf 学生认证  发表于 2013-9-23 12:19:33 |只看作者 |坛友微信交流群
PhoenixHuang 发表于 2013-9-22 22:49
let me try:

data test;
嗯,我后来用allcomb, 好像实现起来还比较快捷,你的方法也不错:

data _null_;
   array x[5] $3 ('ant' 'bee' 'cat' 'dog' 'ewe');
   n=dim(x);
   k=3;
   ncomb=comb(n,k);
   do j=1 to ncomb+1;
      call allcomb(j, k, of x
  • );
          put j 5. +3 x1-x3;
       end;
    run;
    SAS writes the following output to the log:
        1   ant bee cat
        2   ant bee ewe
        3   ant bee dog
        4   ant cat dog
        5   ant cat ewe
        6   ant dog ewe
        7   bee dog ewe
        8   bee dog cat
        9   bee ewe cat
       10   dog ewe cat
       11   dog ewe cat
  • 使用道具

    报纸
    edwardzxf 学生认证  发表于 2013-9-23 12:20:31 |只看作者 |坛友微信交流群
    邓贵大 发表于 2013-9-23 00:41
    1. Functions allcomb, allperm
    http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default ...
    嗯,谢谢,我后来自己找到了这种方法,挺不错的

    使用道具

    地板
    jingju11 发表于 2013-9-24 05:14:35 |只看作者 |坛友微信交流群
    使用allcomb()方程是非常美妙的程序.在以前我也有此需要,但是没有静下来考虑是否可以用宏来编写.今天决心试一试.

    我的程序此处可见
    但是,在运行超过计13个字符串的时候,有BUG.也许大家有什么意见.
    京剧

    使用道具

    7
    邓贵大 发表于 2013-9-24 12:50:24 |只看作者 |坛友微信交流群
    jingju11 发表于 2013-9-24 05:14
    使用allcomb()方程是非常美妙的程序.在以前我也有此需要,但是没有静下来考虑是否可以用宏来编写.今天决心试 ...
    why bother with slower recursions?
    There's an iterative approach
    http://support.sas.com/techsup/technote/ts498.html
    Be still, my soul: the hour is hastening on
    When we shall be forever with the Lord.
    When disappointment, grief and fear are gone,
    Sorrow forgot, love's purest joys restored.

    使用道具

    8
    jingju11 发表于 2013-9-25 00:32:54 |只看作者 |坛友微信交流群
    indeed. thanks

    使用道具

    9
    jingju11 发表于 2013-9-25 00:41:37 |只看作者 |坛友微信交流群
    邓贵大 发表于 2013-9-24 12:50
    why bother with slower recursions?
    There's an iterative approach
    http://support.sas.com/techsup/ ...
    的确如此.
    感觉楼主真有一览众山小的意味.无论提出问题还是解决问题,都非常不凡.
    值得学习.
    京剧

    使用道具

    10
    邓贵大 发表于 2013-9-25 03:14:18 |只看作者 |坛友微信交流群
    jingju11 发表于 2013-9-25 00:41
    的确如此.
    感觉楼主真有一览众山小的意味.无论提出问题还是解决问题,都非常不凡.
    值得学习.
    京大师过誉了,看不出你程序的毛病
    但有个笨方法
    1. %macro comb(
    2.         k,
    3.         words,
    4.         out=
    5. );
    6. %local i;
    7. %if &k=0 %then %do;
    8.         data &out;
    9.                 length x $ 200;
    10.                 x=' ';
    11.         run;
    12. %end;
    13. %else %if &k=%sysfunc(countw(&words)) %then %do;
    14.         data &out;
    15.                 length x $ 200;
    16.                 x = "&words";
    17.                 output;
    18.         run;
    19. %end;
    20. %else %do;
    21.         %local tmpdsn1 tmpdsn2;
    22.         data;stop;run;        %let tmpdsn1=%substr(&syslast, 6);
    23.         data;stop;run;        %let tmpdsn2=%substr(&syslast, 6);
    24.         %comb(%eval(&k-1), %substr(&words, %index(&words, %str( ))), out=&tmpdsn1)
    25.         %comb(&k, %substr(&words, %index(&words, %str( ))), out=&tmpdsn2)
    26.         data &out;
    27.                 set &tmpdsn1(in=in1) &tmpdsn2;
    28.                 if in1 then x = catx(' ', scan("&words",1), x);
    29.         run;
    30.         proc datasets nolist;
    31.                 delete &tmpdsn1 &tmpdsn2;
    32.         quit;
    33. %end;
    34. %exit:
    35. %mend;

    36. options nonotes;
    37. %comb(0,one two three four five six seven eight nine ten 11 12 13,out=hell)
    38. proc append base=hell1 data=hell;
    39. %comb(1,one two three four five six seven eight nine ten 11 12 13,out=hell)
    40. proc append base=hell1 data=hell;
    41. %comb(2,one two three four five six seven eight nine ten 11 12 13,out=hell)
    42. proc append base=hell1 data=hell;
    43. %comb(3,one two three four five six seven eight nine ten 11 12 13,out=hell)
    44. proc append base=hell1 data=hell;
    45. %comb(4,one two three four five six seven eight nine ten 11 12 13,out=hell)
    46. proc append base=hell1 data=hell;
    47. %comb(5,one two three four five six seven eight nine ten 11 12 13,out=hell)
    48. proc append base=hell1 data=hell;
    49. %comb(6,one two three four five six seven eight nine ten 11 12 13,out=hell)
    50. proc append base=hell1 data=hell;
    51. %comb(7,one two three four five six seven eight nine ten 11 12 13,out=hell)
    52. proc append base=hell1 data=hell;
    53. %comb(8,one two three four five six seven eight nine ten 11 12 13,out=hell)
    54. proc append base=hell1 data=hell;
    55. %comb(9,one two three four five six seven eight nine ten 11 12 13,out=hell)
    56. proc append base=hell1 data=hell;
    57. %comb(10,one two three four five six seven eight nine ten 11 12 13,out=hell)
    58. proc append base=hell1 data=hell;
    59. %comb(11,one two three four five six seven eight nine ten 11 12 13,out=hell)
    60. proc append base=hell1 data=hell;
    61. %comb(12,one two three four five six seven eight nine ten 11 12 13,out=hell)
    62. proc append base=hell1 data=hell;
    63. %comb(13,one two three four five six seven eight nine ten 11 12 13,out=hell)
    64. proc append base=hell1 data=hell;
    65. proc print data=hell1;
    66. options notes;
    复制代码
    Be still, my soul: the hour is hastening on
    When we shall be forever with the Lord.
    When disappointment, grief and fear are gone,
    Sorrow forgot, love's purest joys restored.

    使用道具

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

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

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

    GMT+8, 2024-6-17 18:33