使用" PROC FORMAT"重新排列字符列。在SAS

时间:2015-04-20 16:07:55

标签: format sas

我想采用以下数据变量:

"Nebraska-Iowa"
"Washington-Arkansas"
"Illinois-Utah"

并对其进行转换,以便它将连字符周围的字符组按字母顺序排序:

"Iowa-Nebraska"
"Arkansas-Washington"
"Illinois-Utah"

有一种简单的方法吗?我需要在连字符周围分割字符串,必要时重新排列,然后重新粘贴在一起。

更新

在玩了Matthew的回答之后,我决定将这个概括为具有以下数据集的任意数量的州:

Nebraska-Iowa
Washington-Arkansas-Texas
Illinois-Utah
Colorado

这是我正在尝试构建的代码。我正在努力的是构建一个我循环的数组,拉出相应的单词,然后在安排后将它们粘贴在一起。请帮忙!

/*Example dataset*/
data have;
    format text $50.;
    input text;
    datalines;  
Nebraska-Iowa
Washington-Arkansas-Texas
Illinois-Utah
Colorado
run;

/*Rearrange strings in dataset*/
data arrangestrings;
    set have;
    length result $50;
    howmanyb = countc(text,'-');
    howmany = howmanyb + 1;
    array state[howmany] _character_;
    do i=1 to howmany;
         state[i] = scan(text, i, '-');
    end;
    call sortc(of state(*));
    result = catx("-", state[*]);
    keep result;
run; 

1 个答案:

答案 0 :(得分:3)

我认为你不需要为这样的任务定义用户定义的格式。内置的scan方法是您的朋友:

data have;
    format text $50.;
    input text;
    datalines;  
Nebraska-Iowa
Washington-Arkansas
Illinois-Utah
run;


data want;
    set have;
    length word1 word2 result $50;
    word1 = scan(text, 1, '-');
    word2 = scan(text, 2, '-');
    result = ifc(word1 <= word2, text, catx('-', word2, word1));
run;

proc print data=want;
run;

如果您不熟悉,请查看我使用的内置函数(scanifccatx)的文档:

http://support.sas.com/documentation/cdl/en/allprodslang/67244/HTML/default/viewer.htm#syntaxByType-function.htm