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

Hello all - I ran into an obstacle when using proc transpose module:

The structure of input data is as follows - segment is the index / unique identifier, followed by a series of metrics including revenue, inventory, expense, etc. Each metric has 12 columns representing monthly values for a 1-year period. The goal is to rotate the dataset so that metric values are listed vertically for further data steps.

SegmentRev1Rev2Rev12Inv1Inv2Inv12Exp1Exp2Exp12
Seg1....................................
Seg2....................................
Seg3....................................
.......................................
Seg10....................................

Here is the code I used:

proc transpose data = input_data out = output_data;

by segment;

run;

The output is almost what I hoped for but missing one series of metric - inventory. Notice how the rows cover revenue and expense but not inventory metrics for each segment. It seems as if SAS skipped Inv for a very particular reason (which I can't figure out what). Anyone experienced something similar? I have tried with simplified input dataset in order to locate the root cause, however am getting the same incomplete results. Any thoughts would be greatly appreciated. Thanks in advance.

Segment_NAME_COL1 (metric value)
Seg1Rev1
Seg1Rev2
Seg1Rev3
Seg1Rev4
Seg1Rev5
Seg1Rev6
Seg1Rev7
Seg1Rev8
Seg1Rev9
Seg1Rev10
Seg1Rev11
Seg1Rev12
Seg1Exp1
Seg1Exp2
Seg1Exp3
Seg1Exp4
Seg1Exp5
Seg1Exp6
Seg1Exp7
Seg1Exp8
Seg1Exp9
Seg1Exp10
Seg1Exp11
Seg1Exp12
Seg2Rev1
Seg2Rev2
Seg2Rev3
Seg2Rev4
Seg2Rev5
Seg2Rev6
Seg2Rev7
Seg2Rev8
Seg2Rev9
Seg2Rev10
Seg2Rev11
Seg2Rev12
Seg2Exp1
Seg2Exp2
Seg2Exp3
Seg2Exp4
Seg2Exp5
Seg2Exp6
Seg2Exp7
Seg2Exp8
Seg2Exp9
Seg2Exp10
Seg2Exp11
Seg2Exp12

Many thanks!

Tao

1 ACCEPTED SOLUTION

Accepted Solutions
arodriguez
Lapis Lazuli | Level 10

Hi,

If you use the VAR option to indicate wich variable do you like to transpose of the proc transpose should be solved.

proc transpose data = input_data out = output_data;

by segment;

var  Rev1-Rev12 Inv1-Inv12 Exp1-Exp12;

run;

View solution in original post

9 REPLIES 9
ballardw
Super User

Mixture of numeric and character variables comes to mind as a likely issue.

Maybe a row of example data that exhibits the problem would help diagnose more specific issues.

arodriguez
Lapis Lazuli | Level 10

Hi,

If you use the VAR option to indicate wich variable do you like to transpose of the proc transpose should be solved.

proc transpose data = input_data out = output_data;

by segment;

var  Rev1-Rev12 Inv1-Inv12 Exp1-Exp12;

run;

stlimpbizkit
Calcite | Level 5

Many thanks for the suggestion. It solved the incomplete result issue. For continuity, please allow me to expand the question slightly further beyond proc transpose:


What if the number of month is dynamic instead of constant? In the actual case, I defined a macro variable counting number of months that should be included:


%let m = 12*(year(end_date) - year(start_date)) + (month(end_date) - month(start_date)) + 1;


And then, I refer to variable m when calling for variables in proc transpose: var Rev1-Rev&m.


In this case, SAS will insert the whole macro logic after "Rev" which then would consider Rev as a undefined function, instead of Rev12 that I was going for.


How do I go around this?

arodriguez
Lapis Lazuli | Level 10

I think that should work with var Rev1-Rev&m always that you apply the %eval function. Try something like this

var Rev1-Rev%eval(&m);

stlimpbizkit
Calcite | Level 5

Hi arodriguez, many thanks for the advice. I am now one step closer, with one error message to solve for within %eval function:

"A character operand was found in the %EVAL function or %IF condition where a numeric operand is required."

Here's the code I used:

%let m = 12*(year(end_date) - year(start_date)) + (month(end_date) - month(start_date)) + 1;

proc transpose data =input_date out = output_date;

by segment;

var

rev1-rev%eval(&m)

inv1-inv%eval(&m)

exp1-exp%eval(&m)

run;

I also tried the following attempting to force &m into numeric format but no luck:

A.     rev1-rev%eval(input(&x,4.0));

B.     rev1-rev%eval(&x*1);


:smileyconfused::smileyconfused::smileyconfused:

:smileyconfused::smileyconfused::smileyconfused:

:smileyconfused::smileyconfused::smileyconfused:

arodriguez
Lapis Lazuli | Level 10

Are you calculating "m" on a data step? Because in your calculation you don't have "&"

try to calculate it in a data step such this;

data test;

m = 12*(year(end_date) - year(start_date)) + (month(end_date) - month(start_date)) + 1;

call symputx('m',m);

run;


And check that "m" is well calculated. The symputx should transform the "m" value to macro value

stlimpbizkit
Calcite | Level 5

Beautiful! I learned quite a few tricks from this. Many thanks.

Reeza
Super User

If rev, exp and inv are the only variables that start with that prefix you can use the colon shortcut.

proc transpose data = input_data out = output_data;

by segment;

var  Rev: Inv: Exp:;

run;

stlimpbizkit
Calcite | Level 5

This works too. Thanks!

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
  • 9 replies
  • 1071 views
  • 9 likes
  • 4 in conversation