在SAS中单独解析标题行 [英] Parsing header line separately in SAS

查看:12
本文介绍了在SAS中单独解析标题行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有输入文件,其中第一行有标题信息.(制表符分隔的数据值).在这些值中,有一个整数值指定应如何解析文件的其余部分.如果该值小于阈值,则应以一种方式解析文件,否则如果该值大于阈值,则应以不同方式解析数据.

I have input file, in which the first line has header information. (Data values which are tab-separated). Amongst these values, there is an integer value that specifies how the rest of the file should be parsed. If this value is less than a threshold value, then the file should be parsed in one way, otherwise if the value is greater that the threshold value, then the data should be parsed in a different way.

%let isNew = Undefined;

data header;
infile "&infile" OBS=1;
INPUT Agency $ Status $ Num $ fdate sdate;
if fdate < 20130428 then
 %let isNew = false;
else
 %let isNew = true;
run;

data factors;
infile "&infile" missover FIRSTOBS=2 LRECL=1000;
if isNew = false then
 input
   @1   poolno                  $6.
   @7   factor                  9.8 @;
else
 input
   @3   poolno                  $6.
   @9   factor                  9.8 @;

(some more input parsing code)

在上面的代码中,我定义了一个变量 isNew 并根据条件检查将该变量设置为真/假.在后续的数据块中,我是根据这个变量的值来决定文件的解析方式.

In the above code, I have defined a variable isNew and setting that variable to true/false depending on the condition check. In the subsequent data block, I am using the value of this variable to decide which way the file is to be parsed.

一个示例输入文件是(要检查的值以粗体显示):

A sample input file is (the value to be examined is in bold):

FHLMC 更新 #1 20130130 20130306

FHLMC UPDATE #1 20130130 20130306

138788024201321000

138788024201321000

140379000000000000

140379000000000000

我是 SAS 新手.请问有什么建议吗?

I am new to SAS. Any suggestions please?

推荐答案

你把数据步长变量和宏变量搞混了.您不能有条件地执行这样的 %LET 语句(而且您不想这样做).CALL SYMPUT 是您创建宏变量的方式,如果您愿意的话;但在这种情况下,您不需要,因为您可以在一个数据步骤中执行所有操作:

You are mixing up data step variables and macro variables. You can't conditionally execute a %LET statement like that (and you don't want to). CALL SYMPUT is how you'd create a macro variable, if you wanted to; but in this case you don't, as you can perform this all in one data step:

data want;
informat fdate sdate YYMMDD8.;
retain Agency Status Num fdate sdate;
if _n_ = 1 then do;
  input Agency $ Status $ Num $ fdate sdate;
end;
else do;
    if fdate < '28APR2013'd then input
       @1   poolno                  $6.
       @7   factor                  9.8;
    else
     input
       @3   poolno                  $6.
       @9   factor                  9.8;

    output;
end;
datalines;
MyAgency MyStatus MyNum 20130401 20130501
MyPool123456783123
MyPoo2435678904123
;;;;
run;

现在,需要注意的一件事 - 您通常不会将小数添加到信息中(即,@9 因子 9.8),除非您有一个没有小数的数字并且您想要添加它.IE,在我创建的数据中,我没有小数点(123456783),它会加一个(1.23456783).但是,如果小数点已经在数据中,即 1.23456783 在您的数据文件中,那么您只需将其输入为 9.让 SAS 为您放置小数点.此外,如果您对此进行测试,请确保将数据线移动到第一列(将它们缩进 4 会导致此操作失败).

Now, one thing to note - You normally don't add the decimal to the informat (ie, @9 factor 9.8) unless you have a number that has no decimal and you want to add it. IE, in the data I created, I did not have a decimal (123456783) and it will add one (1.23456783). However, if the decimal is already in the data, ie 1.23456783 is in your data file, then you should just input it as 9. and let SAS place the decimal for you. Also, if you test this make sure to move the datalines over to the first column (indenting them 4 will cause this to fail).

另外,您需要学习如何使用 SAS 日期 - 看看我是如何稍微改变它的.

Also, you need to learn how to work with SAS dates - see how I changed it slightly.

这篇关于在SAS中单独解析标题行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆