在 SAS 中创建一个带有日期的宏变量
Create a Macro variable with dates in SAS
我有两个数据集——一个有 accountid,每个 accountid 对应不同的日期。数据看起来像这样
Accountid
Account open_date
1234567
21st Nov 2020
1254268
30 Nov 2020
第二个数据是交易级别的每日数据,看起来像这样每个账户 ID 都有每日余额。
Accountid
transaction_date
Customer_Bal
1234567
21st Nov 2020
400£
1254267
22 Nov 2020
100£
1254268
22 Nov 2020
50£
1254268
23 Nov 2020
0
1254268
24 Nov 2020
20£
1254268
25 Nov 2020
45£
有 50 个具有不同开户日期的不同账户
我想创建一个宏代码,它从第一个 table 中获取不同的帐户 ID,然后从第二个 table 中获取从开户日期到接下来十天的余额。
我已经为一个日期创建了宏,然后如何创建从该帐户开立日期起接下来的十天
proc sql;
select distinct account_open_date into :exec_date from abc
order by account_open_date;
data _null_;
CALL SYMPUT('run0',put(intnx('day',&run.,0,'s'), yymmddn8.));
CALL SYMPUT('run1',put(intnx('day',&run.,1,'s'), yymmddn8.));
CALL SYMPUT('run2',put(intnx('day',&run.,2,'s'), yymmddn8.));
CALL SYMPUT('run3',put(intnx('day',&run.,3,'s'), yymmddn8.));
CALL SYMPUT('run4',put(intnx('day',&run.,4,'s'), yymmddn8.));
CALL SYMPUT('run5',put(intnx('day',&run.,5,'s'), yymmddn8.));
CALL SYMPUT('run6',put(intnx('day',&run.,6,'s'), yymmddn8.));
CALL SYMPUT('run7',put(intnx('day',&run.,7,'s'), yymmddn8.));
CALL SYMPUT('run8',put(intnx('day',&run.,8,'s'), yymmddn8.));
CALL SYMPUT('run9',put(intnx('day',&run.,9,'s'), yymmddn8.));
CALL SYMPUT('run10',put(intnx('day',&run.,10,'s'), yymmddn8.));
run;
如何将所有不同的 account_open_dates 存储在宏中,然后为每个帐户 ID 从第二个 table 中取出接下来十天的交易
您确实不需要宏来进行此处理。
一个简单的 SQL 联接将在一个 table 中获得您需要的一切,可以在使用 [=13] 的 PROC
或 DATA
步骤中进一步处理=]声明。
示例:
假定日期变量包含 SAS 日期值(从 SAS 日期纪元开始的天数整数)。
proc sql;
create table first_ten_days as
select
accounts.account_id
, accounts.open_date
, transactions.transaction_date
, transactions.balance
from accounts
join transactions
on accounts.account_id = transactions.account_id
where transactions.transaction_date - accounts.open_date <= 10
order by account_id, transaction_date
;
proc ...
by account_id;
...
理查德说得对,正如所解释的那样,您实际上并不需要为此进行宏处理,但假设您确实需要它。
您要做的是编写您的宏以获取 一个 帐户 ID,然后 运行 代码如下:
%macro pull_records(account_id=);
%local exec_date;
proc sql;
select distinct account_open_date into :exec_date from abc
order by account_open_date
where account_id = "&account_id"
;
data want;
set transactions;
where account_id = "&account_id."
and transaction_date between (&exec_date.) and (&exec_date.+10);
run;
... whatever else you are doing ...
%mend pull_records;
proc sql;
select distinct cats('%pull_records(account_id=',account_id,')')
into :pull_list separated by ' '
from accounts
;
quit;
&pull_list.;
宏提取自己特定的开放日期,然后用它做任何您想做的事。但是,如果您不在每个帐户上分别做事,那么一定要按照理查德的方式去做,这确实是唯一的好方法。
我有两个数据集——一个有 accountid,每个 accountid 对应不同的日期。数据看起来像这样
Accountid | Account open_date |
---|---|
1234567 | 21st Nov 2020 |
1254268 | 30 Nov 2020 |
第二个数据是交易级别的每日数据,看起来像这样每个账户 ID 都有每日余额。
Accountid | transaction_date | Customer_Bal |
---|---|---|
1234567 | 21st Nov 2020 | 400£ |
1254267 | 22 Nov 2020 | 100£ |
1254268 | 22 Nov 2020 | 50£ |
1254268 | 23 Nov 2020 | 0 |
1254268 | 24 Nov 2020 | 20£ |
1254268 | 25 Nov 2020 | 45£ |
有 50 个具有不同开户日期的不同账户
我想创建一个宏代码,它从第一个 table 中获取不同的帐户 ID,然后从第二个 table 中获取从开户日期到接下来十天的余额。
我已经为一个日期创建了宏,然后如何创建从该帐户开立日期起接下来的十天
proc sql;
select distinct account_open_date into :exec_date from abc
order by account_open_date;
data _null_;
CALL SYMPUT('run0',put(intnx('day',&run.,0,'s'), yymmddn8.));
CALL SYMPUT('run1',put(intnx('day',&run.,1,'s'), yymmddn8.));
CALL SYMPUT('run2',put(intnx('day',&run.,2,'s'), yymmddn8.));
CALL SYMPUT('run3',put(intnx('day',&run.,3,'s'), yymmddn8.));
CALL SYMPUT('run4',put(intnx('day',&run.,4,'s'), yymmddn8.));
CALL SYMPUT('run5',put(intnx('day',&run.,5,'s'), yymmddn8.));
CALL SYMPUT('run6',put(intnx('day',&run.,6,'s'), yymmddn8.));
CALL SYMPUT('run7',put(intnx('day',&run.,7,'s'), yymmddn8.));
CALL SYMPUT('run8',put(intnx('day',&run.,8,'s'), yymmddn8.));
CALL SYMPUT('run9',put(intnx('day',&run.,9,'s'), yymmddn8.));
CALL SYMPUT('run10',put(intnx('day',&run.,10,'s'), yymmddn8.));
run;
如何将所有不同的 account_open_dates 存储在宏中,然后为每个帐户 ID 从第二个 table 中取出接下来十天的交易
您确实不需要宏来进行此处理。
一个简单的 SQL 联接将在一个 table 中获得您需要的一切,可以在使用 [=13] 的 PROC
或 DATA
步骤中进一步处理=]声明。
示例:
假定日期变量包含 SAS 日期值(从 SAS 日期纪元开始的天数整数)。
proc sql;
create table first_ten_days as
select
accounts.account_id
, accounts.open_date
, transactions.transaction_date
, transactions.balance
from accounts
join transactions
on accounts.account_id = transactions.account_id
where transactions.transaction_date - accounts.open_date <= 10
order by account_id, transaction_date
;
proc ...
by account_id;
...
理查德说得对,正如所解释的那样,您实际上并不需要为此进行宏处理,但假设您确实需要它。
您要做的是编写您的宏以获取 一个 帐户 ID,然后 运行 代码如下:
%macro pull_records(account_id=);
%local exec_date;
proc sql;
select distinct account_open_date into :exec_date from abc
order by account_open_date
where account_id = "&account_id"
;
data want;
set transactions;
where account_id = "&account_id."
and transaction_date between (&exec_date.) and (&exec_date.+10);
run;
... whatever else you are doing ...
%mend pull_records;
proc sql;
select distinct cats('%pull_records(account_id=',account_id,')')
into :pull_list separated by ' '
from accounts
;
quit;
&pull_list.;
宏提取自己特定的开放日期,然后用它做任何您想做的事。但是,如果您不在每个帐户上分别做事,那么一定要按照理查德的方式去做,这确实是唯一的好方法。