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

Below is the DS "wide". 

 

data wide;
input subj$ x1 x2 x3 x4 x5 y1 y2 y3 y4 y5;
cards;
001 8 5 6 5 4 10 20 30 40 50
002 7 5 6 4 5 11 33 29 34 56
003 2 2 4 5 6 22 38 21 20 34
;
run;

 

I need output using proc transpose. First 10 obs of LONG DB should look like this.

 

Subj Time X Y

001   1      8 10

001   2      5 20

001   3      6 30

001   4      5 40  

001   5      4 50

002   1      7 11

002   2      5 33

002   3      6 29

002   4      4 34

002   5      5 56

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Why do you NEED to use proc transpose? The problem is solved more easily with a data step:

 

data want (keep=subj time x y);
  set wide;
  array _x x1-x5;
  array _y y1-y5;
  do time=1 to 5;
    x=_x(time);
    y=_y(time);
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

Why do you NEED to use proc transpose? The problem is solved more easily with a data step:

 

data want (keep=subj time x y);
  set wide;
  array _x x1-x5;
  array _y y1-y5;
  do time=1 to 5;
    x=_x(time);
    y=_y(time);
    output;
  end;
run;

Art, CEO, AnalystFinder.com

 

adityaa9z
Obsidian | Level 7
Thanks Art. This works.

Can you please help me understand this. Still a learner.

x=_x(time);
y=_y(time);

What does _x and _y do?
art297
Opal | Level 21

If you look at the code:

data want (keep=subj time x y);
  set wide;
  array _x x1-x5;
  array _y y1-y5;
  do time=1 to 5;
    x=_x(time);
    y=_y(time);
    output;
  end;
run;

_x is the name assigned to an array that will contain the five values of x1 thru x5

_y is the name assigned to an array that will contain the five values of y1 thru y5

 

a do loop is then used to create the variable TIME and, within the loop, the values of x and y are assigned the values contained in the 1st thru fifth elements of the two arrays.

 

BTW, here is how you could do the same thing using proc transpose (but using more code):

 

proc transpose data=wide out=needx (rename=(_name_=_time col1=x));
  var x1--x5;
  by subj;
run;

proc transpose data=wide out=needy (rename=(_name_=_time col1=y));
  var y1--y5;
  by subj;
run;

data want (drop=_:);
  set needx;
  set needy;
  time=input(compress(_time,'y'),8.);
run;

data want;
  retain subj time x y;
  set want2;
run;

 

HTH,

Art, CEO, AnalystFinder.com

 

ilikesas
Barite | Level 11

Hi art297,

 

I would like to ask you a small question about the code please.

 

I see that the variable "subj" isn't included in the loop, but nonetheless each subj is repeated 5 times - so that means that when you set the data "wide", the subsequent loop is applied to each observation of the original data "wide", as if there is a loop within a loop?

 

Thanks!

art297
Opal | Level 21

@ilikesas: subj, x1, x2, x3, x4, x5, y1, y2, y3, y4, and y5 were all populated by the set statement and would have been present in the output file, but I excluded them by including a keep option in the file declared by the data statement.

The do loop was accomplished within each iteration. Thus, yes, it is like a loop within a loop because the normal datastep is, in fact, a loop where _n_ defines which iteration (record) the code is working on.

 

Art, CEO, AnalystFinder.com

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
  • 5 replies
  • 1089 views
  • 3 likes
  • 3 in conversation