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

How should I write code so I can have program A behave as program B?

Program A: 

 

data test;
  <insert code here>;
  cards;
7  8.3  10.5  9  5.1  8.2  10.2  10.3  7.1  5.9
7.9  8.2  9.6  9  6.5  7.3  10.2  10.6  6.3  5.2
run;

Program B:

 

data test;
  input x y;
  cards;
7    7.9
8.3  8.2
10.5 9.6
9    9
5.1  6.5
8.2  7.3
10.2 10.2
10.3 10.6
7.1  6.3
5.9  5.2
;
run;

Thanks for any hint.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

The most simple application of PROC TRANSPOSE:

data have;
input var1-var10;
datalines;
7  8.3  10.5  9  5.1  8.2  10.2  10.3  7.1  5.9
7.9  8.2  9.6  9  6.5  7.3  10.2  10.6  6.3  5.2
;

proc transpose data=have out=long (drop=_name_);
var var:;
run;

View solution in original post

7 REPLIES 7
whymath
Barite | Level 11
I have tried "input #1 x @ #2 y @;", just one row is successfully readed. >_<
Kurt_Bremser
Super User

The most simple application of PROC TRANSPOSE:

data have;
input var1-var10;
datalines;
7  8.3  10.5  9  5.1  8.2  10.2  10.3  7.1  5.9
7.9  8.2  9.6  9  6.5  7.3  10.2  10.6  6.3  5.2
;

proc transpose data=have out=long (drop=_name_);
var var:;
run;
whymath
Barite | Level 11
Fine but is there a more awesome answer?
Kurt_Bremser
Super User
data _null_;
dowhatiwant;
run;

😉

 

Really, the TRANSPOSE is the most simple and flexible code; in a single data step, you would have to write repeating LAGx() functions for all variables (repeated for n-1 times, where n is the number of observations in the source dataset). Which means you need an additional step anyway to get the observation number.

Or you load the whole dataset into a hash object and transpose it from there, which also means LOTS of code.

Let SAS do the work for you.

whymath
Barite | Level 11
Very reasonable, I'll recept your solution.
FreelanceReinh
Jade | Level 19

@whymath wrote:
Fine but is there a more awesome answer?

Hi @whymath,

 

Do you mean a solution using a single (more complicated) DATA step like this?

data test;
infile cards col=c;
input @;
_n=countw(_infile_,' ');
do _i=1 to _n;
  input #1 @_x x @;
  _x=c;
  input #2 @_y y @;
  _y=c;
  output;
end;
drop _:;
cards;
7  8.3  10.5  9  5.1  8.2  10.2  10.3  7.1  5.9
7.9  8.2  9.6  9  6.5  7.3  10.2  10.6  6.3  5.2
;
whymath
Barite | Level 11
Impressive! I hardly use the option 'col='. Thank you very much, my friend.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 1925 views
  • 0 likes
  • 3 in conversation