DATA Step, Macro, Functions and more

Re: Transpose

Accepted Solution Solved
Reply
Regular Contributor
Posts: 229
Accepted Solution

Re: Transpose

Hi....I am trying to create a new variable by transposing the original results and join the transposed results together to create the new variable. I understand I can use Proc Transpose to get the results below. Where I am having problem is how to handle the situation when number of new transposed variables varies for each client. Any suggestions would be greatly appreciated.....Thanks.

Original:

ClientResults
A112
A114
A111
A115
B110
B113
B114
C116
C114
C112
C111

Client Final

A1     12:14:11:15

B1    10:13:14

C1     16:12:14:12:11


Accepted Solutions
Solution
‎07-17-2015 10:29 AM
Super User
Posts: 11,338

Re: Transpose

Since your transpose would create variables such as col1, col2, etc:

data want;

     set transposed;

     array c col: ;

     final = catx(':', of c(*));

run;

which will ignore any of the Col that are blank for a given client.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,228

Re: Transpose

data have;

input Client $ Results;

datalines;

A1 12

A1 14

A1 11

A1 15

B1 10

B1 13

B1 14

C1 16

C1 14

C1 12

C1 11

;

data want(keep=client final);

set have;

length Final $ 25;

retain final;

by client;

Final=catx(':',final,put(results,2.));

if first.client then final=put(results,2.);

if last.client;

run;

Valued Guide
Posts: 860

Re: Transpose

I did this:

data have;

infile cards dsd;

input Client$    Results$;

cards;

A1,12

A1,14

A1,11

A1,15

B1,10

B1,13

B1,14

C1,16

C1,14

C1,12

C1,11

;

run;

proc transpose data=have out=tran (drop=_NAME_);by client;var results;

data want(keep=client final);

set tran;

Final = catx(':',col1,col2,col3,col4);

run;

Then stepped away and like your solution much better.  Can you put into words with this line is doing?  if first.client then final=put(results,2.);

Super User
Super User
Posts: 7,037

Re: Transpose

Posted in reply to Steelers_In_DC

CATX(':',of COLSmiley Happy

Respected Advisor
Posts: 3,799

Re: Transpose

Posted in reply to Steelers_In_DC

It is resetting the value of FINAL (retained) for the first.client.  It would make more sense to move it up one line and init FINAL to missing.  And even better to use DO UNTIL.

data want(keep=client final);
   set have;
   length Final $ 25;
  
retain final;
   by client;
   if first.client then call missing(final);
   Final=catx(':',final,put(results,2.));
  
if last.client;
   run;
proc print;
  
run;

data want(keep=client final);    
  
do until(last.client);
      set have;
      by client;
      length Final $ 25;
      Final=catx(
':',final,put(results,2.));
     
end;
  
run;
proc print;
  
run;
Trusted Advisor
Posts: 1,228

Re: Transpose

Posted in reply to Steelers_In_DC

It resets final variable for each client and terminates cumulative process.

Super User
Super User
Posts: 7,037

Re: Transpose

It might work but it seems strange to first set FINAL to have the full list from the previous by group plus the first value from this by group and later overwrite it with the first value from this group.

Trusted Advisor
Posts: 1,228

Re: Transpose

Thanks for your input. Agreed, it should be in reverse order with call missing as suggested by data_null_ above.

Solution
‎07-17-2015 10:29 AM
Super User
Posts: 11,338

Re: Transpose

Since your transpose would create variables such as col1, col2, etc:

data want;

     set transposed;

     array c col: ;

     final = catx(':', of c(*));

run;

which will ignore any of the Col that are blank for a given client.

Regular Contributor
Posts: 229

Re: Transpose

Hi....I tried your suggestion and I am getting message:

 

WARNING: Defining an array with zero elements.

Super User
Posts: 11,338

Re: Transpose

The example I posted assumed you had run the data through Proc Transpose and that the transposed variables were named Col1 Col2 Col3 etc, as the default behavior.

If you transpose and use a different prefix name, use that instead of COL:

Regular Contributor
Posts: 229

Re: Transpose

Hi Ballardw...It works now.....used "array c c:;" and it worked exactly like you said would....thank you so much...its really appreciated.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 429 views
  • 8 likes
  • 6 in conversation