我在文本文件中有如下所示的数据。
如何将文本文件划分为3个数据集/表?
1有收益数据,第2有赎回数据,第3有到期数据。它们中的每一行都有很多行,我刚才只提到了每一行3-4行。我试图使用Infile语句,但不知道如何拆分。这里的想法是:首先,初始数据(earnings)将被读取,当sas标识单词redemptions时,它必须停止,其余的数据必须转到第二个数据集,每当sas标识单词Expirations时,该关键字下面的数据必须转到第三个数据集。有什么建议吗?
Earnings
abc 123 xyz abjjdd
bhb edw ajd jnjnjknn
ebc ecc cec cecekckk
....
redemptions
abc 123 xyz abjjdd
bhb edw ajd jnjnjknn
ebc ecc cec cecekckk
Expirations
abc 123 xyz abjjdd
bhb edw ajd jnjnjknn
ebc ecc cec cecd ccsdc
djc c djc cjdcjjnc发布于 2016-05-17 02:23:28
使用retain变量将帮助您实现这一目标。
使用下面的代码,只需将infile语句中的infile替换为文件名,并设置正确的infile参数。
data rawImport;
infile datalines dsd delimiter=' ' truncover;
informat C1-C4 $32.;
input C1-C4;
datalines;
Earnings
abc 123 xyz abjjdd
bhb edw ajd jnjnjknn
ebc ecc cec cecekckk
Redemptions
abc 234 xyz abjjdd
bhb edw ajd jnjnjknn
ebc ecc cec cecekckk
Expirations
abc 345 xyz abjjdd
bhb edw ajd jnjnjknn
ebc ecc cec cecd ccsdc
djc c djc cjdcjjnc
;通过使用retain变量,我们现在可以将行分配到适当的数据集。
data Earnings Redemptions Expirations;
set rawImport;
length outputDS $ 12;
retain outputDS;
* Determine output dataset;
if C1 = "Earnings" then do;
outputDS = "Earnings";
delete;
end;
else if C1 = "Redemptions" then do;
outputDS = "Redemptions";
delete;
end;
else if C1 = "Expirations" then do;
outputDS = "Expirations";
delete;
end;
* output to appropriate dataset;
if outputDS = "Earnings" then output Earnings;
else if outputDS = "Redemptions" then output Redemptions;
else if outputDS = "Expirations" then output Expirations;
drop outputDS;
run;日志现在显示:
NOTE: There were 13 observations read from the data set WORK.RAWIMPORT.
NOTE: The data set WORK.EARNINGS has 3 observations and 4 variables.
NOTE: The data set WORK.REDEMPTIONS has 3 observations and 4 variables.
NOTE: The data set WORK.EXPIRATIONS has 4 observations and 4 variables.https://stackoverflow.com/questions/37263695
复制相似问题