The SAS Output Delivery System and reporting techniques

retain multiple variables within group

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 114
Accepted Solution

retain multiple variables within group

Hi SAS community-

 

I would like to retain multiple variables within groups.  I can figure out how to retain 1 variable, but cannot get my code to do the 2nd variable in the way that I would like within groups.  Here's current sas code along with my 'want' dataset. My starting dataset has just the 3 variables: id, count and date.

 

Thanks!

 

proc sort data=have; by id;

data want;

set have;

by id;

retain date_r1-date_r3;

if first.id then date_r1=date;

????

 

run;

 

data have;

input id count date date_r1 date_r2 date_r3;

datalines;

aa 1 01/20/2014 . . .

aa 2 03/24/2015 01/20/2014 . .

aa 3 04/23/2015 01/20/2014 03/24/2015 .

bb 1 10/23/2013 . . .

bb 2 03/24/2015 10/23/2013 . .

cc 3 08/10/2014 . . .

dd . . . . .

dd 2 05/19/2014 . . .

ee 1 03/02/2014 . . .

ee 2 03/19/2013 03/02/2014 . .

ee 3 04/23/2015 03/02/2014 03/19/2013 .

ee 4 10/12/2015 03/02/2014 03/19/2013 04/23/2015

 


Accepted Solutions
Solution
‎11-13-2017 02:19 PM
Esteemed Advisor
Posts: 5,392

Re: retain multiple variables within group

Posted in reply to sophia_SAS

Not easy to understand the question. Maybe this:

 

data have;
input id $ count date :mmddyy10.;
format date yymmdd10.;
datalines;
aa 1 01/20/2014
aa 2 03/24/2015
aa 3 04/23/2015
bb 1 10/23/2013
bb 2 03/24/2015
cc 3 08/10/2014
dd . . 
dd 2 05/19/2014
ee 1 03/02/2014
ee 2 03/19/2013
ee 3 04/23/2015
ee 4 10/12/2015
;

data want;
array date_r{3};
format date_r: yymmdd10.;
do i = 1 by 1 until(last.id);
    set have; by id;
    output;
    if i <= dim(date_r) then date_r{i} = date;
    end;
drop i;
run;

proc print noobs; var id count date date_r1-date_r3; run;
PG

View solution in original post


All Replies
Super Contributor
Posts: 320

Re: retain multiple variables within group

Posted in reply to sophia_SAS

Not sure without seeing the desired data set ( WANT). Simple  use of "????" does not show what you need. Will it work if you use a DO - satement like:

 

if first.id then DO;

      date_r1=date;

      date_r2 = date; /* NOT SURE WHAT IS TO BE ASSIGNED */

      date_r3 = date; /* NOT SURE WHAT IS TO BE ASSIGNED */

END;

Trusted Advisor
Posts: 1,822

Re: retain multiple variables within group

Posted in reply to sophia_SAS

It is not clear what output you want.

 

Ususally I declare RETAIN with new-temporary variable names.

It is useles to declare retain of input variable name, like yours date_r1 - date_r3.

 

Anyway, you can assign more then one retained variable, using DO statement,

something like:

data want;
  set have;
   by id;
        retain var1 - var3;
       if first.id then do;
           var1 = date_r1;
           var2 = date_r2;
           var3 = date_r3;
      end;
       else do;    /* 2nd to last obs in a group */
           ......
       end;
      if last.id then output;    /* or any other function(s) */
run;

Please post yout desired output, in order to get more efficient code.

Solution
‎11-13-2017 02:19 PM
Esteemed Advisor
Posts: 5,392

Re: retain multiple variables within group

Posted in reply to sophia_SAS

Not easy to understand the question. Maybe this:

 

data have;
input id $ count date :mmddyy10.;
format date yymmdd10.;
datalines;
aa 1 01/20/2014
aa 2 03/24/2015
aa 3 04/23/2015
bb 1 10/23/2013
bb 2 03/24/2015
cc 3 08/10/2014
dd . . 
dd 2 05/19/2014
ee 1 03/02/2014
ee 2 03/19/2013
ee 3 04/23/2015
ee 4 10/12/2015
;

data want;
array date_r{3};
format date_r: yymmdd10.;
do i = 1 by 1 until(last.id);
    set have; by id;
    output;
    if i <= dim(date_r) then date_r{i} = date;
    end;
drop i;
run;

proc print noobs; var id count date date_r1-date_r3; run;
PG
Frequent Contributor
Posts: 114

Re: retain multiple variables within group

Thank you! This is exactly what I was looking for in the code.  Apologies for the lack of clarity in my initial post.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 312 views
  • 0 likes
  • 4 in conversation