Sas 如何将一个变量拆分为多行
我想得到的是通过取这个变量的子集来分离Sas 如何将一个变量拆分为多行,sas,Sas,我想得到的是通过取这个变量的子集来分离reason\u code变量,每个reason只有4个字符,并且总是组合2个字母和2个数字 我要获取的数据集如下所示: **Application_id Reaon_code Value** 123 AB31AB45 £500 124 AB43RD49TY87
reason\u code
变量,每个reason只有4个字符,并且总是组合2个字母和2个数字
我要获取的数据集如下所示:
**Application_id Reaon_code Value**
123 AB31AB45 £500
124 AB43RD49TY87 £640
125 RT87 £900
126 CD19RV29 £1000
希望这有意义
第二个问题,我想创建一个显示:
Application_id Reason_code Value
123 AB31 £500
123 AB45 £500
124 AB43 £640
124 RD49 £640
124 TY87 £640
145 RT87 £900
假设所有代码都有4个字符,那么一个简单的DO循环就可以完成这项工作。继续使用前四个字符,直到字符串为空。如果创建一个长度仅为4的变量,并为其指定一个较长的字符串,则只适合前四个字符。然后,可以使用SUBSTR()函数在下次循环之前删除前四个字符
Application_id Reason_code Value Waterfall_reason Unique_Reason
123 AB31 £500 1 (as it his AB31 first) 0 (as it hits both AB31 and AB45)
123 AB45 £500 0 (as it hits AB31 first) 0 (as it hits both AB31 and AB45)
124 AB43 £640 1 (as it hits AB43 first) 0 (as it hits both AB43,RD49 and TY87)
124 RD49 £640 0 0
124 TY87 £640 0 0
145 RT87 £900 1 (as it hits RT87 first) 1 (as it ONLY Hit RT87)
数据已经存在;
输入ID原因代码:$20。价值
卡;
123 AB31AB45 500
124 AB43RD49TY87 640
125 RT87 900
126 CD19RV29 1000
;;;;
数据需求;
设置有(重命名=(原因代码=原因列表));
长度原因\代码$4瀑布\原因8唯一\原因8;
unique_reason=length(reason_list)这里是另一种使用正则表达式的方法,基于不同的假设,即子字符串基于字母+数字,而不是固定的4字符设置。下面的代码将一个接一个地拾取符合字母+数字模式的字符串(在这种情况下,将包括2个字母+2个数字),直到输入字符串的整个长度用完为止。”瀑布_-reason'仅在拾取第一个子字符串后才被标记,而'unique_-reason'由countw()使用字母作为分隔符来完成
Data have;
informat Application_id $3. Reaon_code $100. Value NLMNLGBP.;
input Application_id Reaon_code Value;
Format Value NLMNLGBP.;
cards;
123 AB31AB45 £500
124 AB43RD49TY87 £640
125 RT87 £900
126 CD19RV29 £1000
;
Data Want;
format Application_id $3. Reason_code $4. Value NLMNLGBP.;
set have;
OrigCode = Reaon_Code;
Keep Application_id Reason_code Value ;
Do Start = 1 to 25 by 4;* an arbitrary high number;* you could use a do while or a do until, also.;
Reason_code = Substr( Reaon_Code , start , 4 ) ;
if reason_code = '' then leave;
output;
end;
run;
您好,skybook-请展示您迄今为止为澄清您的想法所做的努力,以及哪些可能对其他人有所帮助-而不仅仅是列出要求请您解释一下?是否不需要在某个地方定义\u pos
?
Data have;
informat Application_id $3. Reaon_code $100. Value NLMNLGBP.;
input Application_id Reaon_code Value;
Format Value NLMNLGBP.;
cards;
123 AB31AB45 £500
124 AB43RD49TY87 £640
125 RT87 £900
126 CD19RV29 £1000
;
Data Want;
format Application_id $3. Reason_code $4. Value NLMNLGBP.;
set have;
OrigCode = Reaon_Code;
Keep Application_id Reason_code Value ;
Do Start = 1 to 25 by 4;* an arbitrary high number;* you could use a do while or a do until, also.;
Reason_code = Substr( Reaon_Code , start , 4 ) ;
if reason_code = '' then leave;
output;
end;
run;
data have;
input ID Reason_Code :$20. Value;
cards;
123 ABcd31AB45 500
124 AB43RD49T87 640
125 RT87 900
126 C19RV29 1000
;;;;
data want;
set have;
_pat=prxparse('/[a-z]+[0-9]+/io');
_start=1;
_stop=length(reason_code);
unique_reason=ifn(countw(reason_code,,'a')=1,1,0);
do _n=1 by 1 until (_pos = 0);
call prxnext(_pat,_start,_stop,reason_code,_pos,_len);
new_code=substr(reason_code,_pos, _len);
waterfall_reason=ifn(_n=1,1,0);
if not missing (new_code) then
output;
end;
drop _:;
run;