我正在使用SAS数据步骤查找报告:
我有一个数据集:
Name Company Date
X A 199802
X A 199705
X D 199901
y B 200405
y F 200309
Z C 200503
Z C 200408
Z C 200404
Z C 200309
Z C 200210
Z M 200109
W G 200010
报告我正在寻找:
Name Company From To
X A 1997/05 1998/02
D 1998/02 1999/01
Y B 2003/09 2004/05
F 2003/09 2003/09
Z C 2002/10 2005/03
M 2001/09 2001/09
W G 2000/10 2000/10
谢谢,
尝试使用proc打印,但不准确。所以寻找数据 null 解决方案。
data _null_;
set salesdata;
by name company date;
array x(*) from;
From=lag(date);
if first.name then count=1;
do i=count to dim(x);
x(i)=.;
end;
count+1;
If first.company then do;
from_date1=date;
end;
if last.company then To_date=date;
if from_date1 ="" and to_date="" then delete;
run;
答案 0 :(得分:1)
我已完成数据步骤以计算From_date和To_date
然后proc报告按组打印报告。
proc sort data=have ;
by Name Company Date;
run;
data want(drop=prev_date date);
set have;
by Name Company date;
attrib From_Date To_date format=yymms10.;
retain prev_date;
if first.Company then prev_date=date;
if last.Company then do;
To_date=Date;
From_Date=prev_date;
end;
if not(last.company) then delete;
run;
proc sort data=want;
by descending name ;
run;
proc report data=want;
define Name/order order=data;
run;
答案 1 :(得分:1)
data _null_;
set yourEvents;
by Name Company notsorted;
file print;
If _N_ EQ 1 then put
@01 'Name'
@06 'Company'
@14 'From'
@22 'To'
;
if first.Name then put
@01 Name
@; ** This instructs sas to not start a new line for the next put instruction **;
retain From To;
if first.company then do;
From = 1E9;
To = 0;
end;
if Date LT From then From = Date;
if Date GT To then To = Date;
if last.Company then put
@06 Company
@14 From yymm7.
@22 To yymm7.
;
run;
答案 2 :(得分:1)
恕我直言,最简单的方法是利用.collection
及其proc report
列类型作为下面的代码。请注意,analysis
和name
列会按字母顺序自动排序(正如大多数摘要函数或过程所做的那样)。
company
html输出:
(在SAS 9.4 win7 x64上测试)
============================ OFFTOPIC ================== ============
也可以考虑使用/* your data */
data have;
infile datalines;
input Name $ Company $ Date $;
cards;
X A 199802
X A 199705
X D 199901
y B 200405
y F 200309
Z C 200503
Z C 200408
Z C 200404
Z C 200309
Z C 200210
Z M 200109
W G 200010
;
run;
/* convert YYYYMM to date */
data have2(keep=name company date);
set have(rename=(date=date_txt));
name = upcase(name);
y = input(substr(date_txt, 1, 4), 4.);
m = input(substr(date_txt, 5, 2), 2.);
date = mdy(m,1,y);
format date yymms7.;
run;
/****** 1. proc report ******/
proc report data=have2;
columns name company date=date_from date=date_to;
define name / 'Name' group;
define company / 'Company' group;
define date_from / 'From' analysis min;
define date_to / 'To' analysis max;
run;
或proc means
。基本代码表如下所示。但是,您还可以看到需要进一步调整输出格式。
proc tabulate
proc制表输出:
/***** 2. proc tabulate *****/
proc tabulate data=have2;
class name company;
var date;
table name*company, date=' '*(min='From' max='To')*format=yymms7.;
run;
proc表示输出(不能输出日期格式,不知道为什么):
您可以就改进这些替代方式发表意见。