-1

我正在尝试从文本文件创建 SAS 数据集。文本文件以如下格式显示数据:

-标题标题标题

-标题标题标题

四月春季赛男

$$$$$$$$$$$$$$$$$$$$

姓名年龄状态/这些是文本文件中的标题/

$$$$$$$$$$$$$$$$$$$$

约翰·史密斯 30 CA

马克·多伊 49 TX

五月 SpringRace2 女

$$$$$$$$$$$$$$$

姓名 年龄 状态

$$$$$$$$$$$$$$$

贝蒂怀特 50 ME

简·史密斯 37 纽约


我在数据步骤中遇到的问题是:绕过不同的标题行,然后在 ****** 标题 ******* 之前收集“事件”数据作为变量,然后跳过标题 和为实际的人分配变量。它在整个巨大的文本文件中都是类似的格式。请问有人能指出我正确的方向吗?

我一直在尝试:Data work.test;infile c:\tester dlm=' , $' 遗漏;输入 /// 月 $15。事件名称 $15。性别 6 美元。(这是我卡住的地方,因为我不知道如何跳过文本文件中的“姓名年龄状态”,而只是将变量分配给“John Smith 30 CA”等)运行;

我还认为必须有更好的方法来传递标题,因为不确定它们总是只有 2 行长。

谢谢

4

2 回答 2

0

我认为,如果分隔数据值的标题总是重复并且您知道它们是什么,那么在 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 看到的方式将数据行打印到您的日志中。

于 2015-02-19T00:06:35.673 回答
0

希望您很久以前就解决了您的问题,但这里有另一个建议。在输入语句上使用尾随 @ 可让您应用第二个输入语句,这将是首选解决方案。这个解决方案并没有真正使用尾随@,但我把它留给你将来考虑。

DATA test;
INFILE 'stacktest.txt' lrecl=200 missover;
length n1 n2 n3 n4 $20. ;
input @1 c1 $1. @1 c2 $2. @1 c5 $5. @1 lne & $75. @ ;
keep month event gender fname lname age state;
if c1 = ' ' then return;
if c1 = '-' then return;
if c1 = '$' then return;
if c5 = 'Name' then return;

n1 = scan(lne, 1);
n2 = scan(lne, 2);
n3 = scan(lne, 3);
n4 = scan(lne, -1);

if ( n3 eq 'Male' or n3 eq 'Female')  then do;
   month = n1 ;
   event = n2;
   gender = n3  ;
     return;
     end;
 else do ;
*  input  fname $ lname $ age  state $ ;
fname = n1 ;
lname = n2 ;
age   = n3 ;
state = n4 ;
     output;
      end;
 retain month event gender;
run;
于 2015-03-17T18:45:55.490 回答