简体   繁体   English

在多个数据集中批量格式化日期

[英]Batch formatting of Date in multiple datasets

i have extracted a number of excel spreadsheets into sas using proc import. 我已经使用proc import将许多Excel电子表格提取到了sas中。 however i now need to join the datasets together and need a uniform date format for all datasets. 但是,我现在需要将数据集连接在一起,并且需要所有数据集的统一日期格式。 they are currently in character format and some are structured as '1999Q1' dates and some as '12/02/2013' dates. 它们目前为字符格式,有些结构为“ 1999Q1”日期,有些结构为“ 12/02/2013”​​。 any help on how i can change formats for all dates in all datasets? 关于如何更改所有数据集中所有日期的格式的任何帮助?

You will need to use INPUT() function to convert the strings to dates so that you can merge them. 您将需要使用INPUT()函数将字符串转换为日期,以便可以合并它们。 Let's make some sample datasets to simulate what you might have imported from your Excel sheets. 让我们制作一些示例数据集来模拟您可能已经从Excel工作表中导入的数据集。

data have1;
  date='1999Q1';
  var1=1;
run;
data have2;
  date='02DEC2013'd ;
  format date yymmdd10.; 
  var2=2;
run;

Now let's get the variable names and types from those datasets. 现在,让我们从这些数据集中获取变量名称和类型。

proc contents data=work._all_ noprint out=contents; run;

We can use this metadata to write some code to convert the strings into dates. 我们可以使用此元数据编写一些代码以将字符串转换为日期。

filename code temp;
data _null_;
  set contents;
  where upcase(name)='DATE' and type=2;
  file code ;
  length dsn $41;
  dsn=catx('.',libname,memname);
  put 'data ' dsn ';'
    / '  set ' dsn ';'
    / '  datenum=input(date,anydtdte.);'
    / '  format datenum yymmdd10.;'
    / '  rename datenum=date date=datechar;'
    / 'run;'
  ;
run;

%inc code / source2 ;

Now we can merge the datasets. 现在我们可以合并数据集。

data want ;
  merge have1 have2;
  by date;
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM