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

Hello everyone,

 

This is an example of my problem I have this dataset:

 

john 1 2 3

alex 2 5 6

tito 1 2 6

 

And I want this:

 

john 1

john 2

john 3

alex 2

alex 5

alex 6

tito 1

tito 2

tito 6

 

If you have any idea ( of course I have 100 hundred columns this is just an example).

 

Thanks a lot.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello Alexandra,

 

Try this:

data have;
input name $ x y z;
cards;
john 1 2 3
alex 2 5 6
tito 1 2 6
;

data want;
set have;
array oldvar x--z;
do _i=1 to dim(oldvar);
value=oldvar[_i];
output;
end;
keep name value;
run;

If your variables x, y, z (etc.) are character variables, please define the length of new variable VALUE appropriately using a LENGTH statement. If some are numeric and others are character and you want to have all values in a single character variable VALUE, you have to create two arrays (one for the numeric, one for the character variables) and convert the numeric values to character values by means of the PUT function in the assignment statement (value=...) of one of (then) two DO loops.

 

View solution in original post

5 REPLIES 5
Reeza
Super User
Try Proc Transpose or a data step using arrays.

If you need examples google 'ucla wide to long' or search on here.
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data have;
  a="john"; v1=1; v2=2; v3=3;
run;
proc transpose data=have out=want;
  by a;
  var v:;
run;
FreelanceReinh
Jade | Level 19

Hello Alexandra,

 

Try this:

data have;
input name $ x y z;
cards;
john 1 2 3
alex 2 5 6
tito 1 2 6
;

data want;
set have;
array oldvar x--z;
do _i=1 to dim(oldvar);
value=oldvar[_i];
output;
end;
keep name value;
run;

If your variables x, y, z (etc.) are character variables, please define the length of new variable VALUE appropriately using a LENGTH statement. If some are numeric and others are character and you want to have all values in a single character variable VALUE, you have to create two arrays (one for the numeric, one for the character variables) and convert the numeric values to character values by means of the PUT function in the assignment statement (value=...) of one of (then) two DO loops.

 

alexandralorenzo
Fluorite | Level 6
Thanks a lot
alexandralorenzo
Fluorite | Level 6
The problem with proc transpose that you need a proc sort and take a long time 😕

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1136 views
  • 1 like
  • 4 in conversation