BookmarkSubscribeRSS Feed
mtsasuser
Calcite | Level 5

I was transposing my data from Long to Wide.  It works for most of my data, except for on variable that has duplicates not cleared by the by statement. X=7 and A=W have two observations B=o and B=p.  I know that the array I have written selects the last line where X=7 A=W B=p and does not include the the other X=7 A=W.  Is there a way to write the code to include both B=o and B=p without creating multiple X's either rewriting the DO statement or something else?


XABCD
1qts1
2qts1
2rpts2
3qts1
3rpts2
3wts3
4qts1
4rpts2
4wts3
4tpts4
5qty1
5roty2
5wty3
5tuty4
5vty5
6qth1
6roth2
6spth3
6wpth4
6vth5
6tth6
7zts1
7wots2
7wpts2


data transposed (drop=A B C D) ;

array AA {*} $ A1-A7;

array BB {*} $ B1-B7;

array CC {*} $ C1-C7;

do until (last.X);

set rawdata;

by X;

  aa(d)= a;

  bb(d)=b;

  cc(d)=c;

end;

output;

run;

3 REPLIES 3
M_Maldonado
Barite | Level 11

Hi,

I haven't had a chance to look at your code.

It looks like you might get better input from this community:

I hope it helps.

Miguel

Astounding
PROC Star

It looks like you need to replace the variable D with a better version that changes to "3" on the final observation:

data want;

   set have;

   by X;

   if first.X then d_new = 1;

   else d_new + 1;

run;

Then use D_NEW instead of D to transpose.

Good luck.

Ksharp
Super User

Do not use variable D , make a new variable for that purpose.

Not tested.

data transposed (drop=A B C D) ;

array AA {*} $ A1-A7;

array BB {*} $ B1-B7;

array CC {*} $ C1-C7;

n=0;

do until (last.X);

set rawdata;

by X;

  n+1;

  aa(n)= a;

  bb(n)=b;

  cc(n)=c;

end;

output;

run;

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 choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 819 views
  • 0 likes
  • 4 in conversation