SAS:如何按字母顺序对变量中的单词进行排序?

时间:2016-02-29 15:30:01

标签: sorting sas

我希望得到以下内容:


    data animals;
      length string_in $50;
      infile datalines dlm=",";
      input string_in;
      datalines;
    rattlesnake honeybadger
    lion eagle shark gorilla
    mouse ape horse
    ;
    run;


      data animals_sorted;
        set animals;

        /* magic happens*/

        put string_in;
      run;


Output:
  string_in: honeybadger rattlesnake
  string_in: eagle gorilla lion shark 
  string_in: ape horse mouse

你必须在这里进入什么样的魔法?

1 个答案:

答案 0 :(得分:7)

我不知道任何SAS函数可以对字符串中的单词进行排序,但SAS可以对ARRAY中的变量值进行排序。

data animals;
   length string_in $50;
   infile datalines dlm=",";
   input string_in;
   datalines;
rattlesnake honeybadger
lion eagle shark gorilla
mouse ape horse
;;;;
   run;
data animals_sorted;
   set animals;
   array animals[10] $32 _temporary_;
   call missing(of animals[*]);
   do i = 1 to dim(animals) until(p eq 0);
      call scan(string_in,i,p,l);
      animals[i] = substrn(string_in,p,l);
      end;
   call sortc(of animals[*]);
   length string_out $50;
   string_out = catx(' ',of animals[*]);
   drop i p l;
   run;
proc print;
   run;

enter image description here