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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8111 views
  • 4 likes
  • 5 in conversation