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

Hi I have a table I need to create a process transpose the table looks like this

Date.             Code.               Code Amt 

01/01/16.         A.                   123

01/01/16.         B.                    145

01/01/16.          A.                   55

01/02/16.           C.                145
01/02/16.             A.                55

01/01/16.             B.                  234
01/01/16.           D.                     60

 

i need it to look like this 

 

date.                   Code. A.       Code b.          Code c.         Code d

01/01/16.              178.                379.                  0.                    0

01/02/16             0.                         0                   145.             60

 

thank you in advance 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Or a mix of the 2 solutions above.

 

proc tabulate data=have out=have_sum;

class date code;

var Code_Amt;

table date,code*sum=''*Code_Amt='' ;

run;

proc transpose data=have_sum out=want(drop=_name_) prefix=code_;

by date;

id code;

var Code_Amt_sum;

run;

View solution in original post

6 REPLIES 6
Reeza
Super User

Do you need a report or a dataset?

 

Proc tabulate would work for a report. 

PGStats
Opal | Level 21

If you need a dataset, try this:

 

proc sql;
create table have_sum as
select 
    a.date, 
    b.code, 
    coalesce(sum(code_amt), 0) as code_sum
from 
    (select unique date from have) as a cross join
    (select unique code from have) as b left join 
    have as c on a.date=c.date and b.code=c.code
group by a.date, b.code;
quit;

proc transpose data=have_sum out=want(drop=_name_) prefix=code_;
by date;
id code;
var code_sum;
run;
PG
stat_sas
Ammonite | Level 13

proc tabulate data=have;
class date code;
var Code_Amt;
table date,code*sum=''*Code_Amt='' /misstext='0';
run;

ChrisNZ
Tourmaline | Level 20

Or a mix of the 2 solutions above.

 

proc tabulate data=have out=have_sum;

class date code;

var Code_Amt;

table date,code*sum=''*Code_Amt='' ;

run;

proc transpose data=have_sum out=want(drop=_name_) prefix=code_;

by date;

id code;

var Code_Amt_sum;

run;

PGStats
Opal | Level 21

@ChrisNZ, good idea, but you get missing values and not zeros for missing code sums.

PG
ChrisNZ
Tourmaline | Level 20

@PGStats True. If that's an actual requirement, you can always add the coalesce calls in a third step. It will be very fast on the summarised data. So many options...

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 8178 views
  • 4 likes
  • 5 in conversation