在SAS中查找第一个交易日期

Finding 1st transaction date in SAS

我有 2 个客户 2 个月的交易日期,现在我只需要为该特定客户提取该月的第一个交易日期。像那样我需要一个月的明智。我在 SAS 上对此没有正确的想法。谁能帮忙?提前致谢。

Cust_name Vis_date V 3/1/2016 V 8/1/2016 V 16/1/2016 V 18/1/2016 V 26/1/2016 V 27/1/2016 E 5/1/2016 E 8/1/2016 E 18/1/2016 E 19/1/2016 E 25/1/2016 E 26/1/2016 V 4/2/2016 V 8/2/2016 V 17/2/2016 V 25/2/2016 V 26/2/2016 V 27/2/2016 E 5/2/2016 E 8/2/2016 E 23/2/2016 E 24/2/2016 E 25/2/2016 E 28/2/2016

如果您创建一个跟踪月份的变量,这将变得非常简单! Vis_date 需要格式化为日期变量才能正常工作。

data your_data2;
    set your_data;
    month = month(vis_date);
run;

proc sort data = your_data2;
by cust_name vis_date;
run;

proc sort nodupkey data = your_data2;
by cust_name month;
run;

我会先获取每条记录的月份,然后进行排序。有了它,您可以通过如下数据步骤提取观察结果:

data test.doc1;                                                                                                                         
set test.doc;                                                                                                                           

Month = month(__Vis_date);                                                                                                              
run;                                                                                                                                    

proc sort data=test.doc1;                                                                                                               
by Cust_name Month __Vis_date;                                                                                                          
run;                                                                                                                                    

data test.doc2;                                                                                                                         
set test.doc1;                                                                                                                          
by Cust_name Month;                                                                                                                     
if first.Month then output;                                                                                                             
run;        

您可以在一个 SQL 语句中完成:

proc sql ;
  create table want as
  select Cust_name, 
         put(Vis_date,yymmn6.) as Month, 
         min(Vis_Date) as First_Date format=date9.
  from have 
  group by 1,2
  order by 1,2 ;
quit ;