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:
Client | Results |
---|---|
A1 | 12 |
A1 | 14 |
A1 | 11 |
A1 | 15 |
B1 | 10 |
B1 | 13 |
B1 | 14 |
C1 | 16 |
C1 | 14 |
C1 | 12 |
C1 | 11 |
Client Final
A1 12:14:11:15
B1 10:13:14
C1 16:12:14:12:11
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.
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;
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.);
CATX(':',of COL:)
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.
It resets final variable for each client and terminates cumulative process.
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.
Thanks for your input. Agreed, it should be in reverse order with call missing as suggested by data_null_ above.
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.
Hi....I tried your suggestion and I am getting message:
WARNING: Defining an array with zero elements.
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:
Hi Ballardw...It works now.....used "array c c:;" and it worked exactly like you said would....thank you so much...its really appreciated.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: