BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sophia_SAS
Obsidian | Level 7

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
KachiM
Rhodochrosite | Level 12

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;

Shmuel
Garnet | Level 18

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.

PGStats
Opal | Level 21

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
sophia_SAS
Obsidian | Level 7

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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