带if语句的SAS do循环子字符串索引

带if语句的SAS do循环子字符串索引,sas,Sas,我有一个类似“100111111100001111111111000”的二进制字符串 它在SAS中显示为char变量。 如何捕获从1到0或从0到1的每个更改? 我的想法是 大宗报价 我一直在研究如何编写递归语句。一次处理20000个字符串,每个字符串都可能很长。。。。。。我想我可以 零=索引字符串,“0”;一=索引字符串,“1”;如果零>一,则 字符串=子字符串,零;否则,如果为零 这是正确的方向吗?我应该如何放入DO循环语句 多谢各位 亚伦对我来说似乎很有道理。稍微简化 do position

我有一个类似“100111111100001111111111000”的二进制字符串 它在SAS中显示为char变量。 如何捕获从1到0或从0到1的每个更改? 我的想法是

大宗报价

我一直在研究如何编写递归语句。一次处理20000个字符串,每个字符串都可能很长。。。。。。我想我可以

零=索引字符串,“0”;一=索引字符串,“1”;如果零>一,则 字符串=子字符串,零;否则,如果为零 这是正确的方向吗?我应该如何放入DO循环语句

多谢各位


亚伦对我来说似乎很有道理。稍微简化

do position = 1 to length(String)-1;
  if subpad(string,position,2)='10' then do;
    ... output a row for the 1-0 change ...
  end;
  else if subpad(string,position,2)='01' then do;
    ... output a row for the 0-1 change ...
  end;
end;
当您执行任何想要输出的操作时,我假设将变量设置为“1-0”,然后输出


我在那里使用SUBPAD是出于习惯,只要你正确地检查字符串长度,subscr应该也能正常工作。如果SUBPAD超过字符串末尾,则不会出错。

对我来说似乎合理。稍微简化

do position = 1 to length(String)-1;
  if subpad(string,position,2)='10' then do;
    ... output a row for the 1-0 change ...
  end;
  else if subpad(string,position,2)='01' then do;
    ... output a row for the 0-1 change ...
  end;
end;
Please try these codes to see if this is what you are looking for

data have (keep=type pos);
retain type pos;
x = '100111111100001111111111000';
ct01 = count(x,'01');
ct10 = count(x,'10');

pos = 1;
do i =1 to ct01;
  pos = find(x,'01',pos)+1;
  type='0-1';
  output;
end;

pos = 1;
do i =1 to ct10;
  pos = find(x,'10',pos)+1;
  type='1-0';
  output;
end;

run;

proc sort data=have;
by pos;
run;
当您执行任何想要输出的操作时,我假设将变量设置为“1-0”,然后输出


我在那里使用SUBPAD是出于习惯,只要你正确地检查字符串长度,subscr应该也能正常工作。SUBPAD如果超过字符串的末尾就不会出错。

SUBPAD在我的一生中都在哪里?我有太多的代码检查fpr字符串长度的,空字符串,等等。。。我所需要的只是传票。。。谢谢你的小费,乔!也许我2016年的电子海报将是SUBSTR无法做到的时候。。。SUBSPAD和SUBSTRN都是非常方便的函数。哇,有了SUBSPAD,我就不会得到这些注意:第2413行第3列的SUBSTR函数的第三个参数无效。substr的第二个+第三个参数比字符串长时?谢谢你的提示!非常感谢你的提示!你很敬畏我这辈子都在什么地方?我有太多的代码检查fpr字符串长度的,空字符串,等等。。。我所需要的只是传票。。。谢谢你的小费,乔!也许我2016年的电子海报将是SUBSTR无法做到的时候。。。SUBSPAD和SUBSTRN都是非常方便的函数。哇,有了SUBSPAD,我就不会得到这些注意:第2413行第3列的SUBSTR函数的第三个参数无效。substr的第二个+第三个参数比字符串长时?谢谢你的提示!非常感谢你的提示!你真棒
Please try these codes to see if this is what you are looking for

data have (keep=type pos);
retain type pos;
x = '100111111100001111111111000';
ct01 = count(x,'01');
ct10 = count(x,'10');

pos = 1;
do i =1 to ct01;
  pos = find(x,'01',pos)+1;
  type='0-1';
  output;
end;

pos = 1;
do i =1 to ct10;
  pos = find(x,'10',pos)+1;
  type='1-0';
  output;
end;

run;

proc sort data=have;
by pos;
run;