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

Hi all,

 

I'm attempting to use a proc transpose to move data from a tall set, to a wide set. There is a fairly intensive amount of categories, but the original tall set isn't too large (~74k rows). Here's an example of what said data would look like (I can't share the actual data due to sensitive info) :

Data.PNG

What I want to do is transpose it so the dates become columns, with the spend as each one in its own row, so something like this:

Transpose.PNG

The issue is, whenever I follow any sort of instructions I find anywhere for Transposing, I end up with data that looks like this:

Transposed.PNG

 

The issue it appears is it isn't collapsing down as it transposes, instead just moving each data point for each day over into a separate column in the same row it was previously. My actual data set isn't this neat (as can be expected from about 70k+ lines, which is 30 days of data) but I can remove the identifiers of spend and get only unique values and get it below 30k.

 

I'm a fairly novice programmer in SAS, but I haven't seen this behavior mentioned on any of the transposing tutorials I was following. Any assistance would be helpful, as I'm sure its an easy, obvious issue. Here's my existing SQL (changed to match example rather than my own sensitive data) :

PROC SQL;
     CREATE VIEW WORK.TransposedData AS
           SELECT T.Category1, T.Category2, T.Category3
     FROM WORK.SpendData(FIRSTOBS=1 ) as T
;
QUIT;
PROC TRANSPOSE DATA=TransposedData
     OUT=WORK.TransposedData(LABEL="TransposedData")
     NAME=Source
     LABEL=Label
;
     BY Category1 NOTSORTED Category2 NOTSORTED Category3 NOTSORTED;
     VAR Spend1 Spend2;
     ID   Date;

Thanks for any help possible! I am on SAS EG 7.1, for what it's worth.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

With two variables in the VAR statement of PROC TRANSPOSE, I don't know of a way to get the result you want directly.  However, you could turn the result into what you want with this:

 

proc sort data=TransposedData;

by category1 category2 category3 spendtype;

run;

 

data want;

update transposeddata (obs=0) transposeddata;

by category1 category2 category3 spendtype;

run;

 

Your data sets are small enough that this should be viable.  Good luck.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

With two variables in the VAR statement of PROC TRANSPOSE, I don't know of a way to get the result you want directly.  However, you could turn the result into what you want with this:

 

proc sort data=TransposedData;

by category1 category2 category3 spendtype;

run;

 

data want;

update transposeddata (obs=0) transposeddata;

by category1 category2 category3 spendtype;

run;

 

Your data sets are small enough that this should be viable.  Good luck.

Goose306
Fluorite | Level 6

Thanks for the reply. That seemed to have sorted the stubborn data as I needed.

 

Regarding the two spend categories: I think an easier way will be to break it into two tables then re-merge them together. The main point of needing to massage this data is just make it more logical than a tall list as eventually its going to end up in Excel with a lot of VBA powering it and it needs to be relatively efficent, and so either setting the two spend categories as two separate column sections or as an every other row as I originally intended will make it easier to manage and code for than the current tall data set.

 

Thanks again for your help!

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 1825 views
  • 1 like
  • 2 in conversation