BookmarkSubscribeRSS Feed
CathyVI
Pyrite | Level 9

I saw this sample online and I will like to know the importance of the '&' after the variable 'Location'. Initially, i thought it was used to keep the observation(cole pond, Eagle lake) in one column but I did removed it and still got the same output.

Thanks.

Sample code:

 

options nodate pageno=1 linesize=80 pagesize=40;
data fishdata;
infile datalines missover;
input Location & $10. Date date7.
Length1 Weight1 Length2 Weight2 Length3 Weight3
Length4 Weight4;
format date date7.;
datalines;
Cole Pond 2JUN95 31 .25 32 .3 32 .25 33 .3
Cole Pond 3JUL95 33 .32 34 .41 37 .48 32 .28
Cole Pond 4AUG95 29 .23 30 .25 34 .47 32 .3
Eagle Lake 2JUN95 32 .35 32 .25 33 .30
Eagle Lake 3JUL95 30 .20 36 .45
Eagle Lake 4AUG95 33 .30 33 .28 34 .42
; run;

1 REPLY 1
Tom
Super User Tom
Super User

The & modifier tells the INPUT statement that you need to have two delimiters in a row to mark the end of the next field. This will allow you to read values with embedded single delimiters. 

The reason it didn't matter in your code is because you are using formatted input, so the INPUT statement does not need to try to figure out where the next field ends, it just reads the number of bytes you told it to read.

It is hard to tell what your data lines look like since you let this forum editor flow them into paragraphs. But if your data looks like this:

Cole Pond 2JUN95 31 .25 32 .3 32 .25 33 .3
Cole Pond 3JUL95 33 .32 34 .41 37 .48 32 .28
Cole Pond 4AUG95 29 .23 30 .25 34 .47 32 .3
Eagle Lake 2JUN95 32 .35 32 .25 33 .30
Eagle Lake 3JUL95 30 .20 36 .45
Eagle Lake 4AUG95 33 .30 33 .28 34 .42

Then your formatted more input is not going to work.  The place where second field starts is NOT on the same place on every line. 

The & is also not going to work.  There are NOT two spaces after the end of the LOCATION value. PS Why the heck would anyone be using 2 digits for the year part of a date string?

You should edit the data.  You could make it fixed column (at least for the first two fields). 

Cole Pond  02JUN95 31 .25 32 .3 32 .25 33 .3
Cole Pond  03JUL95 33 .32 34 .41 37 .48 32 .28
Cole Pond  04AUG95 29 .23 30 .25 34 .47 32 .3
Eagle Lake 02JUN95 32 .35 32 .25 33 .30
Eagle Lake 03JUL95 30 .20 36 .45
Eagle Lake 04AUG95 33 .30 33 .28 34 .42

Then this mix of formatted and list mode input styles will work.

input Location $11. Date date7.
      Length1 Weight1 Length2 Weight2 Length3 Weight3 Length4 Weight4
;

Or you could make sure there are at least two spaces after the first field.

Cole Pond   02JUN95 31 .25 32 .3 32 .25 33 .3
Cole Pond   03JUL95 33 .32 34 .41 37 .48 32 .28
Cole Pond   04AUG95 29 .23 30 .25 34 .47 32 .3
Eagle Lake  02JUN95 32 .35 32 .25 33 .30
Eagle Lake  03JUL95 30 .20 36 .45
Eagle Lake  04AUG95 33 .30 33 .28 34 .42

Then this list mode only input styles will work. Notice the : modifier which tells the INPUT statement to adjust the width of the informat to match the width of the next field on the line.

input Location &:$10. Date :date7.
      Length1 Weight1 Length2 Weight2 Length3 Weight3 Length4 Weight4
;

If your current input is working with your current data (at least for the first field) then you might find it easiest to just add the colon modifier to the date informat so that four digit years or extra spaces don't cause any trouble.

input Location $10. Date :date.
      Length1 Weight1 Length2 Weight2 Length3 Weight3 Length4 Weight4
;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 302 views
  • 2 likes
  • 2 in conversation