Exploring, predicting and reporting with SAS Visual Analytics and SAS Visual Statistics

How to find the first record date and last record date for each group

Reply
SAS Employee
Posts: 6

How to find the first record date and last record date for each group

I'm dealing with disease data which has two variabes: GroupID and Infection_date. For example:

GroupIDInfection_date
A2014/01/03
A2014/02/10
A2014/03/05
B2014/01/01
B2014/02/01
B2014/03/15
B2014/03/15

Role type of GroupID is category and role type of Infection_date is date.

Now, I want to create a table contains the first infection date and last infection date for each GroupID like below:

GroupIDFirstLast
A2014/01/032014/03/15
B2014/01/012014/03/15

I tried some different ways but none of them success. Dose any one have experience with this? Thanks a lot.

Super User
Super User
Posts: 7,942

Re: How to find the first record date and last record date for each group

Hi,

Variety of ways spring to mind, this would be my choice:

proc sql;

  create table WANT as

  select  COALESCE(A.GROUPID,B.GROUPID) as GROUPID,

          A.INFECTION_DATE as FIRST,

          B.INFECTION_DATE as LAST

  from    (select GROUPID,MIN(INFECTION_DATE) as INFECTION_DATE from HAVE group by GROUPID) A

  full join (select GROUPID,MAX(INFECTION_DATE) as INFECTION_DATE from HAVE group by GROUPID) B

  on      A.GROUPID=B.GROUPID;

quit;

Super User
Posts: 5,499

Re: How to find the first record date and last record date for each group

An easy way, assuming that DATE is actually numeric (SAS doesn't have a DATE type):

proc summary data=have nway;

   var infection_date;

   class groupID;

   output out=want (keep=groupID first last) min=first max=last;

run;

You might need to apply a format to FIRST and LAST to view them as dates.

Respected Advisor
Posts: 3,156

Re: How to find the first record date and last record date for each group

Another SQL:

proc sql;

     create table WANT as

           select  GROUPID, MIN(INFECTION_DATE) as FIRST, MAX(INFECTION_DATE) as LAST

                FROM HAVE

                     GROUP BY GROUPID

     ;

quit;

Haikuo

SAS Employee
Posts: 6

Re: How to find the first record date and last record date for each group

Thanks for all of your replies!

I forgot to mention that my platform is SAS Visual Analytics. So what I actually need is to build a "Report" in SAS VA 6.4 without any coding. All your suggestion are good on BASE SAS but not what I need. Thanks for your help again.

Contributor
Posts: 40

Re: How to find the first record date and last record date for each group

Hi

You just have to duplicate the date variable and rename it first date. Then change the aggregation to minimum.

The same goes for last date.

But you cant use the variable in filters.

BTW: There are a lot of helpful people in here, but they never notice that it is Visual Analytics forum, and always come up with answers in BASE SAS.

SAS Employee
Posts: 6

Re: How to find the first record date and last record date for each group

Thanks Allan

I've tried your method earlier, but only measure have aggregation option, date type variable doesn't.

Another method I'd try is use "TreatAs" operator to create new variable from Infection_date. This can correctly change date variable into measure. But it comes up another two problem:

1. I can't create max or min for each group. Aggregation work only when we use object with group role, but object "Table" doesn't have this concept.

2. Even if we create min and max successfully. Since these two variables are measure, we can't give them any date-like format.

then I stuck.....

Contributor
Posts: 40

Re: How to find the first record date and last record date for each group

Sorry, I forgot that you can't change format to date.

So, yes - you are stuck.  :smileycry:

Super User
Posts: 3,252

Re: How to find the first record date and last record date for each group

How did you get your data into VA in the first place? Can you derive what you need outside of VA and re-import it?

SAS Employee
Posts: 6

Re: How to find the first record date and last record date for each group

Our raw data is a sas dataset and store infection_date as numeric variable with yymmdd10. format.

VA will auto change numeric variable with date-like format into "date" category.

So make some change before import data can't solve it as I know.Smiley Sad


Super User
Posts: 3,252

Re: How to find the first record date and last record date for each group

I was thinking along the lines of importing into VA the maximum and minimum dates for each group as described above. Would that help you with your issue? You should at least be able to filter on these.

SAS Employee
Posts: 6

Re: How to find the first record date and last record date for each group

Thanks Kiwi. It seems we can only use this preprocess method to achieve our goal. But this method against the spirit of VA, so we still looking for a better way.

SAS Employee
Posts: 19

Re: How to find the first record date and last record date for each group

screenshot_865.png

In Version Visual Analytics 7.1, You will be able to use first and last function as part of the calculation editor.  This will be in Designer and Explorer.  You must have a by group otherwise it would have been a default aggregation.  Look for Visual Analytics 7.1 to release in October.  This will be in the calculation editor with more aggregated values as well.  More details to come.

Regards,

Ted Werner

SAS Employee
Posts: 6

Re: How to find the first record date and last record date for each group

Posted in reply to ted_werner_sas_com

Thanks Ted!!

Occasional Learner
Posts: 1

Re: How to find the first record date and last record date for each group

Posted in reply to ted_werner_sas_com

Hi Ted,

 

We're now using 7.3 and I'm still trying to solve this puzzle. Am I doing something wrong or is it still not possible?

 

Thanx!

Ask a Question
Discussion stats
  • 14 replies
  • 5138 views
  • 0 likes
  • 8 in conversation