我正在进行SAS练习,其中包含以下格式的数据:
3496 Jerry Nelson 13960 Wilson Dr. San Diego CA 92191 40 4
3498 Scott Mason 9226 College Dr. Oak View CA 93022 95 2
3498 CA 35 3
3498 CA 35 11
3500 Michele Stone 8393 West Ct. Emeryville CA 94608 55 5
3500 CA 70 5
对于每个人,数据一直持续到下一个人的名字。下面的代码非常接近我需要的内容,我认为:
libname Ch4data '\\Client\C$\Users\m210028\Google Drive\Adrian\Self-Study\SAS\Chapter4_data';
Data Ch4data.my_donations;
Infile '\\Client\C$\Users\m210028\Google Drive\Adrian\Self-Study\SAS\Chapter4_data\Donations.dat' MISSOVER;
Array amounts(10);
Array months(10);
Input first_name $ 6 - 19
last_name $ 20 - 33
street_address $ 34 - 58
city $ 59 - 88
state_code $ 89 - 93
zip_code $ 94 - 100
amounts(1) 101 - 105 @ 106
months(1);
end = end1;
If ~(end1) Then
Do;
Input test_char $ 6-6 @;
i = 2;
Do While (0 = ANYALPHA(test_char));
Input amounts(i) 101 - 105 @ 106
months(i);
end = end1;
If ~(end1) Then Input test_char $ 6-6 @;
Else test_char = '';
i = i+1;
End;
End;
Run;
Proc Print Data = Ch4data.my_donations;
Title 'Donations to Coastal Humane Society';
Run;
问题是,我在日志中得到了一张丢失的卡片,而文件中的姓氏Michele并没有将其放入数据集中。我怀疑我检测文件末尾的代码是不正确的。有人能告诉我如何检测文件的结尾吗?SAS文档没有帮助。
非常感谢你抽出时间来!
更新:感谢Tom的评论,现在我可以用下面的代码获得最后一行:
libname Ch4data '\\Client\C$\Users\m210028\Google Drive\Adrian\Self-Study\SAS\Chapter4_data';
Data Ch4data.my_donations;
Infile '\\Client\C$\Users\m210028\Google Drive\Adrian\Self-Study\SAS\Chapter4_data\Donations.dat' MISSOVER END=end1;
Array amounts(10);
Array months(10);
Input first_name $ 6 - 19
last_name $ 20 - 33
street_address $ 34 - 58
city $ 59 - 88
state_code $ 89 - 93
zip_code $ 94 - 100
amounts(1) 101 - 105 @ 106
months(1);
If ~(end1) Then
Do;
Input test_char $ 6-6 @;
i = 2;
Do While (0 = ANYALPHA(test_char));
Input amounts(i) 101 - 105 @ 106
months(i);
If ~(end1) Then Input test_char $ 6-6 @;
Else test_char = '';
i = i+1;
End;
End;
Run;
Proc Print Data = Ch4data.my_donations;
Title 'Donations to Coastal Humane Society';
Run;
不幸的是,它没有得到第二行到最后一句。在这个问题上,它跳过了很多第一行的记录。有什么想法?
发布于 2019-10-16 20:31:25
你想把阅读和转换结合起来。它可能更容易阅读,先读,然后转接。事实上,你只需阅读
data step1;
Infile example truncover ;
Input first_name $ 6 - 19
last_name $ 20 - 33
street_address $ 34 - 58
city $ 59 - 88
state_code $ 89 - 93
zip_code $ 94 - 100
amount 101 - 105
month 105 - 110
;
if not missing(first_name) then case+1;
run;
然后应用后继的名称等。
data step2;
update step1(obs=0) step1;
by case;
output;
run;
然后转过来。
data want;
do row=1 by 1 until(last.case);
set step2;
by case;
array months [10];
array amounts [10];
months[row]=month;
amounts[row]=amount;
end;
drop row amount month;
run;
发布于 2019-10-17 04:04:41
当您的名称检查检测到下一个组的第一行时,您将需要使用行保持说明符@@
来保持行。
filename exercise 'c:\temp\exercise.txt';
* create file to read in;
data _null_;
file exercise;
input;
put _infile_;
datalines;
3496 Jerry Nelson 13960 Wilson Dr. San Diego CA 92191 40 4
3498 Scott Mason 9226 College Dr. Oak View CA 93022 95 2
3498 CA 35 3
3498 CA 35 11
3500 Michele Stone 8393 West Ct. Emeryville CA 94608 55 5
3500 CA 70 5
run;
* read-in the data;
* error will occur if data file has a group with more than 10 months of data;
data want;
infile exercise end=end_of_data ;
array amounts(10);
array months(10);
input first_name $ 6 - 19
last_name $ 20 - 33
street_address $ 34 - 58
city $ 59 - 88
state_code $ 89 - 93
zip_code $ 94 - 100
amounts(1) 101 - 105
@ 106 months(1);
do i = 2 by 1 while (not end_of_data);
input name_check $ 6-6 @@;
if name_check = ' ' then
input amounts(i) 101-105 @106 months(i);
else
leave; /* jump out of loop
* when control returns to top the input will be of the held line
*/
end;
run;
https://stackoverflow.com/questions/58419938
复制相似问题