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?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 680 views
  • 2 likes
  • 4 in conversation