我认为,如果分隔数据值的标题总是重复并且您知道它们是什么,那么在 INPUT 语句中使用 @'my_char_string' 列指针会对您有所帮助。例如:
INFILE mydatafile FLOWOVER FIRSTOBS=2;
输入月份 $ 种族 $ 性别 $ @'State' first_name $ last_name $ 地址 $;
INFILE 语句中的 FIRSTOBS=2 选项跳过 HEADER HEADER... 行,而 FLOWOVER 选项告诉 SAS 继续在下一行查找数据,尤其是 @'State'。您可能需要指定其他选项和格式,具体取决于您的输入文件格式、分隔符等。
根据您的编辑,您可以使用月份值来确定您正在阅读事件的开始,然后使用尾随 @、保留和一些条件逻辑,在单独的行中读取参与者并保留参与者之间的事件信息,像这样(只需在第一个 if 子句中添加所有剩余的月份名称):
data test1;
length test $20 month $20 event $20 gender $20 firstname $20 lastname $20 state $2;
infile "test1.txt" DLM=' $' FIRSTOBS=5;
retain month event gender; * Keep these values from last readin;
input test $ @; /* Read in the first word in the data line being
read into test var, and stay on this line for
now (with @)*/
if strip(test) in('April', 'May') then do; /* If test var contains month,
then read in all of the variables,
and skip the name/age/state titles row*/
input @1 month $ event $ gender $ @'State' firstname $ lastname $ age state $ ;
end;
else do; /* Otherwise, the data line being read in should contain
only names, age and state, so read in those values only.
The month, event and gender values will be kept the same
by the retain statement above.*/
input @1 firstname $ lastname $ age state $ ;
end;
drop test; /* Comment out this drop statement to see whats in test var*/
run;
此代码适用于每个事件的不同数量的参与者。但是不能缺少月份才能使此代码正常工作。
有用的提示:要查看 SAS 正在读取的当前数据行中的内容,请尝试添加
put _INFILE_;
在 INFILE 语句之后。它将以 SAS 看到的方式将数据行打印到您的日志中。