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

Hello All,

 

I have a dataset as below. An account can have max 4 different dob/surnames. That is fixed.

Account DoB Surname

100A 01-Dec-17 ABC

100A 02-Dec-17 DEF

100A 03-Dec-17 GEH

100A 04-Dec-17 IJK

200B 01-May-17 LMN

200B 02-May-17 OPQ

300A 05-Jun-17 RST

 

Now, I want my output to look like

Account G1_Surname G1_DoB       G2_Surname   G2_DoB         G3_Surname    G3_DoB        G4_Surname  G4_DoB

100A   ABC        01-Dec-17 DEF         02-Dec-17  GEH         03-Dec-17 IJK         04-Dec-17

200B   LMN        01-May-17 OPQ         02-May-17

300A   RST        05-Jun-17

 

Can you suggest

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Subject was misleading to me for a bit-

/*requesting to frame the question like -- How to restructure  or reshape the dataset using the techniques constructs such as retain ,loop and transpose ? I am dumb sorry as I was wasting my time to see where;'s the loop or you meant the implicit loop or DOW or until end of file*/

 

Anyway:

 

 

 

 data have;

 input  Account $ DoB :date7. Surname $;

 format dob date7.;

 datalines;

100A 01-Dec-17 ABC

100A 02-Dec-17 DEF

100A 03-Dec-17 GEH

100A 04-Dec-17 IJK

200B 01-May-17 LMN

200B 02-May-17 OPQ

300A 05-Jun-17 RST

;

 

data want;

set have;

by account;

retain G1_Surname G1_DoB       G2_Surname   G2_DoB         G3_Surname    G3_DoB        G4_Surname  G4_DoB;

array t(*) $ G1_Surname        G2_Surname            G3_Surname            G4_Surname  ;

array y(*)G1_DoB G2_DoB G3_DoB G4_DoB;

if first.account then do;

     n=1;

     call missing(of t(*),of y(*));

     end;

else n+1;

t(n)=Surname;

y(n)=dob;

if last.account;

format G1_DoB G2_DoB G3_DoB G4_DoB date7.;

drop n DoB Surname  ;

run;

 

 

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

Subject was misleading to me for a bit-

/*requesting to frame the question like -- How to restructure  or reshape the dataset using the techniques constructs such as retain ,loop and transpose ? I am dumb sorry as I was wasting my time to see where;'s the loop or you meant the implicit loop or DOW or until end of file*/

 

Anyway:

 

 

 

 data have;

 input  Account $ DoB :date7. Surname $;

 format dob date7.;

 datalines;

100A 01-Dec-17 ABC

100A 02-Dec-17 DEF

100A 03-Dec-17 GEH

100A 04-Dec-17 IJK

200B 01-May-17 LMN

200B 02-May-17 OPQ

300A 05-Jun-17 RST

;

 

data want;

set have;

by account;

retain G1_Surname G1_DoB       G2_Surname   G2_DoB         G3_Surname    G3_DoB        G4_Surname  G4_DoB;

array t(*) $ G1_Surname        G2_Surname            G3_Surname            G4_Surname  ;

array y(*)G1_DoB G2_DoB G3_DoB G4_DoB;

if first.account then do;

     n=1;

     call missing(of t(*),of y(*));

     end;

else n+1;

t(n)=Surname;

y(n)=dob;

if last.account;

format G1_DoB G2_DoB G3_DoB G4_DoB date7.;

drop n DoB Surname  ;

run;

 

 

Ksharp
Super User

 data have;
 input  Account $ DoB :date7. Surname $;
 format dob date7.;
 datalines;
100A 01-Dec-17 ABC
100A 02-Dec-17 DEF
100A 03-Dec-17 GEH
100A 04-Dec-17 IJK
200B 01-May-17 LMN
200B 02-May-17 OPQ
300A 05-Jun-17 RST
;


proc sql noprint;
select max(n) into : n
 from (select count(*) as n from have group by account);
quit;
proc summary data=have ;
by account;
output out=want idgroup(out[&n] (dob surname)=);
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 3 replies
  • 1841 views
  • 3 likes
  • 4 in conversation