【问题标题】:SAS PROC Import vs DATA step with INFILE带有 INFILE 的 SAS PROC 导入与数据步骤
【发布时间】:2015-04-29 21:45:33
【问题描述】:

当我尝试读取包含地址字段的“~”分隔文件时,我的 PROC IMPORT 步骤正在引发“导入失败”。在 CSV 文件中,5 字节的邮政编码被自动视为数字字段,有时我会收到带有无效邮政编码的错误数据记录,如 VXR1@。遇到这种情况时,我收到“导入失败”错误并且 SAS 作业失败。 PROC IMPORT 会自动转换为带有 infile 的 DATA 步骤。所以我尝试了使用 INFILE 选项以及 INFORMATS 和 FORMATS 的 DATA 步骤,并将 ZIP 的 FORMAT 更改为字母数字。但我现在面临不同的问题。使用 DATA、INFORMAT 和 FORMAT 关键字,会发生长度不匹配,并且数据会自动移动到不同的位置。有人可以帮我找出解决这个问题的方法吗?

包括我使用的 PROC IMPORT 和我在下面使用的 DATA 文件步骤以供参考:

data WORK.TRADER_STATS                               ;
            %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
            infile '/sascode/test/TRADER_STATS.csv' delimiter = '~' MISSOVER DSD lrecl=32767 firstobs=2 ;
               informat TRADER_id best32. ;
               informat dealer_ids $60. ;
               informat dealer_name $27. ;
               informat dealer_city $15. ;
               informat dealer_st $2. ;
               informat dealer_zip $5. ;
               informat SNO best32. ;
               informat start_dt yymmdd10. ;
               informat end_dt yymmdd10. ;
            input
                        TRADER_id
                        dealer_ids $
                        dealer_name $
                        dealer_city $
                        dealer_st $
                        dealer_zip
                        sno
                        start_dt
                        end_dt
           ;
            if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
            run;


proc import file="/sascode/test/TRADER_STATS_BY_DAY.csv" out=WORK.TRADER_STATS_BY_DAY
dbms=dlm replace;
delimiter='~';
;run;

【问题讨论】:

  • 也许您可以使用GUESSINGROWS = n,其中 n 大到足以让 SAS 找到带字母的邮政编码,因此 SAS 将获得正确的数据类型。您有机会分享您的数据吗?
  • 尝试在输入语句中的dealer_zip 之后添加一个$ 符号,尽管这无关紧要。另外,将missover更改为trunco​​ver。

标签: sas


【解决方案1】:

尝试使用: colon operator,这将告诉 SAS 使用提供的信息,但在遇到分隔符时停止读取此变量的值,这将解决您的问题 - 数据自动移动到不同位置

data WORK.TRADER_STATS                               ;
            %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
            infile '/sascode/test/TRADER_STATS.csv' delimiter = '~' MISSOVER DSD lrecl=32767 firstobs=2 ;
            input       TRADER_id : best32.
                        dealer_ids : $60.
                        dealer_name : $27.
                        dealer_city : $15. 
                        dealer_st $ : $2. 
                        dealer_zip : $5. 
                        sno : best32. 
                        start_dt : yymmdd10. 
                        end_dt : yymmdd10.;
            if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
            run;

【讨论】:

  • NEOmen - 谢谢。结肠救了我。同样正如@Reeza 所建议的那样,我使用了 TRUNCOVER 来阻止 EFIERR 标志在我收到不正确的数据时被设置。再次感谢你们俩
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多