BookmarkSubscribeRSS Feed
MikkelVejlby
Calcite | Level 5

Hello.

 

I have a problem with the format I am required to deliver.

 

The data is not problematic: 

CustomerId, AccountID, AccountAmount, AccountStartDate, AccountEndDate, AccountInfoX, AccountInfoY, ...

 

filled with data like 

custA, Acc1, 10, 1/1-2015, 1/1-2025, 'Xa', 'Ya', ...

custA, Acc2, 20, 1/1-2016, 1/1-2026, 'Xb', 'Yb', ...

custA, Acc3, 30, 1/1-2017, 1/1-2027, 'Xc', 'Yc', ...

custB, Acc4, 40, 1/1-2016, 1/1-2021, 'Xd', 'Yd', ...

custB, Acc5, 50, 1/1-2017, 1/1-2022, 'Xe', 'Ye', ...

etc

 

The format I have to deliver is not that easy (for me at least):

custA, Acc1, 10, 1/1-2015, 1/1-2025, 'Xa', 'Ya', ..., Acc2, 20, 1/1-2016, 1/1-2026, 'Xb', 'Yb', ..., Acc3, 30, 1/1-2017, 1/1-2027, 'Xc', 'Yc', ...

custB, Acc4, 40, 1/1-2016, 1/1-2021, 'Xd', 'Yd', ..., Acc5, 50, 1/1-2017, 1/1-2022, 'Xe', 'Ye', ...

etc

Ie for each customer all accounts "to the right" as opposed to one under the other.

 

I have looked at proc transpose, but it doesn't seem to be quite what I need. 

 

Anybody has an idea?

 

Thanks in advance

 

Mikkel

 

version information:

Copyright (c) 2002-2008 by SAS Institute Inc., Cary, NC, USA.
SAS (r) Proprietary Software 9.2 (TS2M3)
Licensed to BNP PF EX LASER SERVEUR 8 CORES AIX MIGRATION, Site xxx.
This session is executing on the AIX 5.3 (AIX 64) platform.

 

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data in the form of a datastep, I neither want to type it out nor guess the structure of the data.  Examples can be found in most posts.

 

As for your required output, it looks like a simple CSV file output, so a datastep like:

data _null_;
  set your data;
  file "youroutput.csv";
  by customerid;
  if first.customer then put customerid;
  put catx(",",accountid,accoutnamount,accountstartdate...);
run;

What I would advise however is to not transpose the data up.  This will make reading in the file so much harder.  The way you have the data now, proc export to CSV would be far easier for the recipient to process.

 

Astounding
PROC Star

You'll need to know something about your data:

 

  • What is the maximum number of observations per customerid
  • What are the lengths of each of your character variables

Then you can code it like this:

 

data want;

array acc {20} $ 10 AccountID1 - AccountID20;

array aca {20} AccountAmount1 - AccountAmount20;

array stdt {20} $ 10 AccountStartDate1 - AccountStartDate20;

array endt {20} $ 10 AccountEndDate1 - AccountEndDate20;

n = 0;

do until (last.customerid);

   set have;

   by customerid;

   n + 1;

   acc{n} = AccountID;

   aca{n} = AccountAmount;

   stdt{n} = AccountStartDate;

   endt{n} = AccountEndDate;

end;

drop n;

run;

 

Obviously, you will need to add more arrays to the program, to account for the additional variables in your data set.  But this is a reasonable framework for transforming a SAS data set.

Peter_C
Rhodochrosite | Level 12
Could be simpler
by retaining the output _file_ buffer until last.customerID
Something like:

Data _null_ ;
file "youroutputfile.csv" dsd lrecl= 32767 ;
Set your.data ;
by customerID ;
if first.customerID then put customerID @@ ;
put AccountID AccountAmount AccountStartDate AccountEndDate AccountInfoX AccountInfoY @@
If last.customerID ;
put ;
run ;

This should generate a .csv file providing what was requested, except perhaps this would have no column headers.
Peter_C
Rhodochrosite | Level 12
Needs a semicolon ... missing from one of the trailing @@

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 1037 views
  • 0 likes
  • 4 in conversation