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

I have a DO loop where I obtain a value and I write it  in a different file, so I obtain something like this:

 

1 0
1 63
1 41
2 39
2 84
2 105
3 87
3 117
3 105

4 147
4 74
4 14

 

How can I separate this second column in different lines to obtain another file like this:

 

1 0 63 41
2 39 84 105

3 87 117 105

4 147 74 14

 

Thank you

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input var var2;
cards;
1 0
1 63
1 41
2 39
2 84
2 105
3 87
3 117
3 105
4 147
4 74
4 14
;

proc transpose data=have out=want;
by var;
var var2;
run;

straight forward proc transpose

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20
data have;
input var var2;
cards;
1 0
1 63
1 41
2 39
2 84
2 105
3 87
3 117
3 105
4 147
4 74
4 14
;

proc transpose data=have out=want;
by var;
var var2;
run;

straight forward proc transpose

mkeintz
PROC Star

If it's raw data and always comes in 3-line sets for each id, you can use some of the features of the INPUT statement:

 

data have (drop=_:);
  input var1 var2_1 / _dummy var2_2 / _dummy var2_3;
cards;
1 0
1 63
1 41
2 39
2 84
2 105
3 87
3 117
3 105
4 147
4 74
4 14
;

 

The '/' tells the input statement to go to the next line.  The variable _DUMMY, which is dropped from the output, is read as the first value of lines 2 and 3.

 

If it's already a SAS data set, then use the proc transpose as per @novinosrin's suggestion.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

@Maider wrote:

I have a DO loop where I obtain a value and I write it  in a different file, so I obtain something like this:

 

1 0
1 63
1 41
2 39
2 84
2 105
3 87
3 117
3 105

4 147
4 74
4 14

 

How can I separate this second column in different lines to obtain another file like this:

 

1 0 63 41
2 39 84 105

3 87 117 105

4 147 74 14

 

Thank you

 

 


It would really help to show the code you used as it is very likely to be a simple modification of that.

By "file" do you mean a SAS data set or an external text file or similar?

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1108 views
  • 2 likes
  • 4 in conversation