定义与在数据步骤可变滤波器和do环 - SAS

问题描述 投票:0回答:2

早上好,我这个问题。

有2个数据集

数据集“ID用户”在那里我有这样的:

id       |  Customer Name   |
-----------------------------
123456   | Michael One      |
123123   | George Two       |
123789   | James Three      |

而第二个数据集命名为“交易”:

id       |  Transaction | Date
-----------------------------------
123456   | Fuel         | 01NOV2018
123456   | Fuel         | 03NOV2018
123123   | Fuel         | 10NOV2018
123456   | Fuel         | 25NOV2018
123123   | Fuel         | 13NOV2018
123456   | Fuel         | 10DEC2018
123789   | Fuel         | 1NOV2018
123123   | Fuel         | 30NOV2018
123789   | Fuel         | 15DEC2018

我想要的结果是创造3分贝像3客户ID,我在指定的第一个数据集已经:

_01NOV2018_15NOV_123456_F
_01NOV2018_15NOV_123123_F
_01NOV2018_15NOV_123789_F

包含:

For  _01NOV2018_15NOV_123456_F :
id       |  Transaction | Date
-----------------------------------
123456   | Fuel         | 01NOV2018
123456   | Fuel         | 03NOV2018

For _01NOV2018_15NOV_123123_F :

id       |  Transaction | Date
-----------------------------------
123123   | Fuel         | 10NOV2018
123123   | Fuel         | 13NOV2018

For _01NOV2018_15NOV_123789_F

empty

我需要创建一个变量数据的步骤,其中一个条款...我怎样才能使这个?

感谢帮助! :)`

loops sas iteration datastep
2个回答
0
投票

HASH OUTPUT方法是创建在DATA步骤运行时动态命名输出数据集的唯一途径。每个问题的评论,你很可能不希望你的原始数据集分成很多内容命名件。无论如何,如过程,在SAS,被称为分割。

你是远更好的服务学习如何在这两个数据步应用WHERE声明和BY组处理,并PROC步骤。

想要的输出似乎被隔离或基于半一个月分类。你可能通过计算含有适当分类值的新semimonth变量,然后使用该下游,例如在PROC PRINT来提供最好的服务。

data customers;
infile cards dlm='|';
attrib
  id length=8
  name length=$20
;
input id name ;
datalines;
123456   | Michael One      |
123123   | George Two       |
123789   | James Three      |
run;

data transactions;
infile cards dlm='|';
attrib
  id length=8
  transaction length=$10
  date length=8 format=date9. informat=date9.
;
input id transaction date;
datalines;
123456   | Fuel         | 01NOV2018
123456   | Fuel         | 03NOV2018
123123   | Fuel         | 10NOV2018
123456   | Fuel         | 25NOV2018
123123   | Fuel         | 13NOV2018
123456   | Fuel         | 10DEC2018
123789   | Fuel         | 1NOV2018
123123   | Fuel         | 30NOV2018
123789   | Fuel         | 15DEC2018
run;

proc sort data=customers;
  by id;
proc sort data=transactions;
  by id date;

* merge datasets and compute semimonth;

data want;
  merge transactions customers;
  by id;

  semimonth = intnx('month',date,0) + 16 * (day(date) > 15);

  attrib semimonth
    format=date9.
    label="Semi-month"
  ;
run;


* process data by semimonth and id, restricting with where;

proc print data=want;
  by semimonth id;
  where semimonth = '01NOV2018'D;
run;

enter image description here


0
投票

你可以通过你的PROC出口代号小宏或只是一个快速过滤器做到这一点。

proc export data=sashelp.class (where=(sex='F')) outfile='/folders/myfolders/females.xlsx' dbms=xlsx replace; run;

proc export data=sashelp.class (where=(sex='M')) outfile='/folders/myfolders/females.xlsx' dbms=xlsx replace; run;

或者你也可以将此转换为小宏:

    %macro exportData(group=);

proc export data=sashelp.class (where=(sex="&group."))
outfile="C:\_localdata\&group..xlsx" 
dbms=xlsx 
replace; 
run;

%mend;

*create list of unique elements to call the macro;
proc sort data=sashelp.class nodupkey out=class;
by sex;
run;

*call the macro once for each group;  

data test;
   set class;
    str = catt('%exportData(group=', sex, ');');
    call execute(str);
run;
© www.soinside.com 2019 - 2024. All rights reserved.