String SAS简化变量的内容

String SAS简化变量的内容,string,sas,simplification,String,Sas,Simplification,在SAS中,我有一个包含以下值的变量V V=1996199619961996200120012001 我想创建这两个变量 V1=19962001 (= different modalities) V2=42 (= the first modality appears 4 times and the second one appears 2 times) 有什么想法吗 谢谢你的帮助 Luc对于第一个问题,如果我正确理解了模式,您可以提取前四个字符和后四个字符: a=子变量,1,4 b=子变量

在SAS中,我有一个包含以下值的变量V

V=1996199619961996200120012001
我想创建这两个变量

V1=19962001 (= different modalities)

V2=42 (= the first modality appears 4 times and the second one appears 2 times)
有什么想法吗

谢谢你的帮助


Luc

对于第一个问题,如果我正确理解了模式,您可以提取前四个字符和后四个字符:

a=子变量,1,4

b=子变量,最大值1,长度变量-3,4

然后,您可以将两者连接起来

c=catsa,b

对于第二种情况,COUNT函数可用于计算字符串中字符串的出现次数:

希望这能有所帮助:

让它更一般一点

某个事件从哪里开始

读取输入

声明输出和工作变量

发现独特的模式并计算它们

在v1和v2中报告结果

我测试的数据


谢谢,但我想概括一下n模式,您应该展示您迄今为止尝试过的代码。
%let modeLength = 4;
%let maxOccur = 100; ** in the input **;
%let maxModes = 10; ** in the output **;
%macro occurStart(occurNo);
    &modeLength.*&occurNo.-%eval(&modeLength.-1)
%mend;
data simplified ;
    infile datalines truncover;
    input v $%eval(&modeLength.*&maxOccur.).;
    format what $&modeLength.. 
           v1   $%eval(&modeLength.*&maxModes.). 
           v2   $&maxModes..;

    array w {&maxModes.}; ** what **;
    array c {&maxModes.}; ** count **;
    countW = 0;
    do vNo = 1 to length(v)/&modeLength.;
        what = substr(v, %occurStart(vNo), &modeLength.);
        do wNo = 1 to countW;
            if what eq w(wNo) then do;
                c(wNo) = c(wNo) + 1;
                goto foundIt;
            end;
        end;
        countW = countW + 1;
        w(countW) = what;
        c(countW) = 1;

        foundIt:
    end;
    do wNo = 1 to countW;
        substr(v1, %occurStart(wNo), &modeLength.) = w(wNo);
        substr(v2, wNo, 1) = put(c(wNo),1.);
        put _N_= v1= v2=;
    end;
    keep v1 v2;
    datalines;
1996199619961996200120012001
197019801990
20011996199619961996200120012001
;
run;