BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

May anyone explain the following code?

What is the using of symbol & in the code?

How many observations we should get ?

 

data two;
input var1 $ var2 & $20.;
cards;
hope  sad and happy
;
2 REPLIES 2
Tom
Super User Tom
Super User

@Ronein wrote:

Hello

May anyone explain the following code?

What is the using of symbol & in the code?

How many observations we should get ?

 

data two;
input var1 $ var2 & $20.;
cards;
hope  sad and happy
;

The purpose of the & in an INPUT statement is to force SAS to looks for two adjacent delimiters to find the end of the next "word" in the line when using LIST MODE input style.  It will also allow you to add an informat specification to your INPUT statement without forcing FORMATTED MODE input style.   

 

Since the second "word" on the line does not have any places with multiple embedded spaces (space is the default delimiter) then for your example it does nothing.  If you had removed the & then SAS would have read VAR using FORMATTED MODE so it would have read the next 20 character, whether or not they contained any number of delimiters.  And since you are reading from in-line data the input is always padded to the next multiple of 80 characters the fact that 'sad and happy' is only 13 characters long would not have any impact.  But if you tried to read that line from a file stead and the line did not have at least 7 spaces after 'happy' then it would read past the end of the line and you would end up with zero observations.  So in that case the & would make a difference as it would prevent the $20. informat from attempting to read past the end of the line.

 

Try this test program:

filename test temp;
options parmcards=test;
parmcards;
123456789012345678901234567890
One   Two  Three  Four
sad and happy
;

data test1;
  infile test;
  input var1 & $20.;
  put var1 = $quote.;
run;

data test2;
  infile test;
  input var1  $20.;
  put var1 = $quote.;
run;

 

 

 

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