BookmarkSubscribeRSS Feed
Chira85
Calcite | Level 5

Dear all,

 

I want to transpose this column structure from round 1 to n:

 

round1

newr

value1

newg

value2

newg

value3

    .

    .

    n

 

to the following one:

Round   newer    newg      newg

1            value1   value2    value3   

.

.

n

 

Thank you for any help!

 

 

4 REPLIES 4
ballardw
Super User

Please provide your data example in the form of a data step. Please note: SAS will not allow you to have two variables with the same name in a data set. So if "newg" is supposed to be a variable your want is not possible.

 

Are there any other variables involved? If so you need to show what happens with them.

You also need to show at least 2 and better 3 "examples" so we can tell what some of the "n" rows of data start with. Such things like is it always exactly 3 sets of values?

 

Also, make sure that what you show is what you mean. The message windows on this forum will reformat text and sometimes what is in the window doesn't quite match what was intended.

Chira85
Calcite | Level 5

Thank you for your response. The data I'am going to transpose is an output from another fortran program. Newg represents 2 variables. The data is:

data transpose;

input iteration;

cards;

round1

newr

value1

newg

value2

newg

value3

round2

newr

value4

newg

value5

newg

value6

round3

newr

value7

newg

value8

newg

value9

;

run;

 

 

 

ballardw
Super User

So you do not actually have a SAS data set yet apparently.

To read the text output that look like that,

assuming you always have exactly 3 groups of values you can read it with something similar to the following code.
The EXACTLY part is important as the / in the input statement below tells SAS to read the next value on the next line. Since SAS cannot have variables with the same name this skips past the newr and newg values as essentially meaningless and counts the rounds. You have have not specified if the "value" are supposed to be character or numeric. The $ in the input indicates to read the data as character. If the values are actually numeric then you remove the $.

data transpose;
infile "path to your text file" ; Retain Round 1; input / / newr $ / / newg1 $ / / newg2 $ ; output; round+1; cards; round1 newr value1 newg value2 newg value3 round2 newr value4 newg value5 newg value6 round3 newr value7 newg value8 newg value9 ; run;

If you are writing the FORTRAN I would strongly suggest using ONE ouput to write the value of Round and the three other variables on one line as that makes much more sense in general.

Chira85
Calcite | Level 5
Thank you very much for your help. It works perfectly!

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