我想从SAS表中检索行级别值(与帐号关联的贷款) - 请在下面找到示例。
输入
Account Number Loans
123 abc, def, ghi
456 jkl, mnopqr, stuv
789 w, xyz
输出
Account Numbers Loans
123 abc
123 def
123 ghi
456 jkl
456 mnopqr
456 stuv
789 w
789 xyz
贷款以逗号分隔,并且没有固定长度。
答案 0 :(得分:1)
countw()
计算一行中的值数,并scan()
将其选中。两者都有一个最后一个可选变量来指定分隔符,在您的情况下为,
。
data Loans (keep= AccountNo Loan);
infile datalines truncover;
Input @1 AccountNo 3. @17 LoanList $250.;
if length(LoanList) gt 240 then put 'WARNING: You might need to extend Loans';
label AccountNo = 'Account Number' Loan = 'Loans';
do loanNo = 1 to countw(LoanList, ',');
Loan = scan(LoanList, loanNo, ',');
output;
end;
datalines;
123 abc, def, ghi
456 jkl, mnopqr, stuv
789 w, xyz
;
proc print data=Loans label noobs;
run;
要启用by AccountNo
处理,我们必须首先从输入构建SAS数据集,然后使用set
语句读回。
data Loans;
infile datalines;
input @1 AccountNo 3. @5 Loan $25.;
datalines;
123 15-abc
123 15-def
123 15-ghi
456 99-jkl
456 99-mnopqr
456 99-stuv
789 77-w
789 77-xyz
;
data LoanLists;
set Loans;
by AccountNo;
现在创建足够长的Loanlist并覆盖SAS的默认行为,以重新初始化每个观察的所有变量(=数据行)。
format Loanlist $250.;
retain Loanlist;
收集帐户的所有贷款,用逗号分隔它们。
if first.AccountNo then Loanlist = Loan;
else Loanlist = catx(', ',Loanlist,Loan);
if length(LoanList) gt 240 then put 'WARNING: you might need to extend LoanList';
仅保留每个帐户的完整列表。
if last.AccountNo;
drop Loan;
proc print;
run;