DATA Step, Macro, Functions and more

input methods

Reply
Contributor
Posts: 33

input methods

Hi, there.

 

I have such question.

If I have sas code as is below and the result should be as shown at the end, what to write instead of the question marks. 

 


data input_method;

input  GG$    HH$     ????????????????;

 

datalines;
G A R I K
H A K O B Y A N
;
run;

////////////////////////////////////////////////////

GG  HH

G   H

A    A

R   K

I    O 

K   B

     Y

     A

     N

Super User
Posts: 6,946

Re: input methods

The datalines need to be written in the same way as the intended result. That's quite obvious, isn't it?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 33

Re: input methods

if dataline contanins huge amount of lines.

 

is there any way to do so or not?

Super User
Super User
Posts: 6,502

Re: input methods

Looks like a simple transpose problem. Not sure where you came up the name for columns in the resulting output.  This will just name them row1, row2, etc .

 

data raw ;
  length row col 8 value $20 ;
  infile cards col=cc length=len truncover ;
  row+1;
  do col=1 by 1 until ( cc > len) ;
    input value @ ;
    output;
  end;
cards;
G A R I K
H A K O B Y A N
Another set of values
;

proc sort data=raw;
  by col row ;
run;

proc transpose data=raw prefix=row out=want(drop=_name_);
  by col ;
  id row ;
  var value ;
run;
Super User
Posts: 5,085

Re: input methods

Here's one approach.  You may benefit by adding LENGTH statements for GG and HH:

 

data input_method;

infile datalines truncover;

input GG_list & $60. / HH_list & $60.;

do _n_=1 by 1 until (GG=' ' and HH=' ');

GG = scan(GG_LIST, _n_);

HH = scan(HH_LIST, _n_);

if GG > ' ' or HH > ' ' then output;

end;

keep gg hh;

datalines;

G A R I K

H A K O B Y A N

;

 

Super User
Posts: 10,516

Re: input methods

[ Edited ]

This will do what you are requesting but is not going to be easily extended to a generic use. The 14 would have to be replaced by the length of the longest line and the better be a space between each character. If the spaces came from a double-byte character set then maybe you wouldn't need the "by 2" bit depending on your current session encoding.

data input_method;
   infile datalines n=2 truncover;
   do i=1 to 14 by 2;
      input #1 @i GG $  #2 @i  HH $ @@ ;
      output;
   end;
   drop i;
input;
datalines;
G A R I K      
H A K O B Y A N
;
run;

If you have an actual application for this then you might look at reading the entire line(s) into variables and parsing.

 

Ask a Question
Discussion stats
  • 5 replies
  • 147 views
  • 4 likes
  • 5 in conversation