BookmarkSubscribeRSS Feed
littlestone
Fluorite | Level 6

Dear All

Suppose I have data:

001F36175180

Variable description for this data is:

ID: columns1-3

Sex: columns 4

Age: columns 5-6

Height: columns 7-9

Weight: columns 10-12

I know I can read in this data using code:

input  

@1 ID $3.

@4 Sex $1.

@5 Age 2.

@7 Height 3.

@10 Weight 3.;

However, the above method is to read data from left to right; my question is, is there a way to read data from right to left, such as:

input  

@12 Weight -3.

@9 Height -3..

@6  Age -2.

@4 Sex -1.

@3 ID -3.;

15 REPLIES 15
art297
Opal | Level 21

Why not?  As long as you use the correct starting positions and formats.  e.g.:

data have;

input 

@10 Weight 3.

@7 Height 3.

@5  Age 2.

@4 Sex $1.

@1 ID $3.;

cards;

001F36175180

;

Art

data_null__
Jade | Level 19

But isn't that still left to right within the fields.  I think "

littlestone" is implying with minus sign in front of informat that the field should be right to left not just the record.  The only thing I can think of is to reverse the value of _INFILE_ but that is a bit awkward.

1799     infile cards;

1800     input @;

1801     _infile_ = reverse(_infile_);

1802     input  Weight 69-71;

1803     put weight=;

1804     list;

1805     cards;

Weight=81

RULE:       ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0

1806                                                                            08157163F100

NOTE: The data set WORK.HAVE has 1 observations and 1 variables.

art297
Opal | Level 21

DN,  Of course you might be correct but, if so, what about the reverse informat?  Art

dhana
Fluorite | Level 6

You can use Column Input to read from right to left.

DATA DSN;

  INPUT WEIGHT 10-12 HEIGHT 7-9 AGE 5-6 SEX $ 4-4 ID 1-3;

DATALINES;

001F36175180

;

RUN;

PROC PRINT DATA = DSN;

RUN;

Thanks

Dhanasekaran R

littlestone
Fluorite | Level 6

thank you all.

I am asking this question because I have a dataset that needs to be read CONDITIONALLY. It seems to me that the data will be easier to manipultae if read from right to left, but, just as data_null_ said, the code  looks kind of awkward. I am just wondering if there is some "nicer looking" code can do the job.

Again, thanks a lot.

kevin_123
Calcite | Level 5

Hi Littlestone.  Are you trying to read the data conditionally, based upon a value in a specific field ? Or is the data in reverse order in the flat file?

If you are trying to read the data based upon a field within the data, such as length then use the @@ to "pause" reading the file.

data results;

 

input 

@1 ID $3.

@4 Sex $1.

@5 Age 2.

@@ ;

if age < 10 then do;

     input  ;   * no height or weight for children under 10;

end;

else do;

     input

@7 Height 3.

@10 Weight 3.;

   end;

cards;

001F36175180

001F01

;

run;

Otherwise,,

just read in the whole line and use the reverse function;

Cheers.

littlestone
Fluorite | Level 6

Thank you very much, Kevin, your answer is very close to what I am looking for.

Actually, part of my problem is that the length of variable Age is not constant (all other variables has defined length). Suppose I have following data:

001F36175180

002F9120100

003F110180160

The age for the above three observations are 36, 9 and 110, respectively. Because of different length of Age (2, 1 and 3, in this example), the starting column for the next variable Height keeps changing, making it difficult to determine where to start reading Height if data has to be read from left to right.

data_null__
Jade | Level 19

If you can determine the length of the record and subtract the lenght of the know fields then the rest is...

filename FT15F001 temp;

data test;

   infile FT15F001 length=l;

   input id $3. sex $1. @;

   w = l-(3+1+(2*3)); *minus know field widths;

   select(w);

      when(1) input age 1. @;

      when(2) input age 2. @;

      when(3) input age 3. @;

      end;

   input (height weight) (3.);

   parmcards;

001F36175180

002F9120100

003F110180160

;;;;

   run;

proc print;

   run;

art297
Opal | Level 21

or, similarly:

data have;

  input

  @1 ID $3.

  @4 Sex $1.;

  ;

  _infile_=strip(reverse(_infile_));

  weight=input(reverse(substr(_infile_,1,3)),3.);

  height=input(reverse(substr(_infile_,4,3)),3.);

  age=input(substr(reverse(_infile_),5,

   length(_infile_)-10),3.);

  cards;

001F36175180

002F9120100

003F110180160

;

run;

FloydNevseta
Pyrite | Level 9

Instead of the Select statement, just read age using the $varying informat.

input id $3. sex $1. @;

w = l-(3+1+(2*3)); *minus know field widths;

input age $varying. w (height weight) (3.);


					
				
			
			
				
			
			
			
			
			
			
			
		
art297
Opal | Level 21

Of course, for l, you mean length(_infile_), but very nice solution!

data_null__
Jade | Level 19

SAS_Bigot wrote:

Instead of the Select statement, just read age using the $varying informat.

input id $3. sex $1. @;

w = l-(3+1+(2*3)); *minus know field widths;

input age $varying. w (height weight) (3.);



I think age should be numeric.  So you will need to read it again with INPUT function.  Six of one, half dozen of the other.

littlestone
Fluorite | Level 6

Thank you all. I really learned a lot.

Howles
Quartz | Level 8

Good that you learned. The person who prepared the file also has things to learn. The design is very bad. Were there two variable-length fields instead of one, it would have been difficult if not impossible to parse.

littlestone wrote:

Thank you all. I really learned a lot.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 15 replies
  • 2675 views
  • 6 likes
  • 8 in conversation