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

Hello,

I am preparing for Base SAS Certification , and need to know basic logic behind the following below question.

Given the text file COLORS.TXT:

----+----1----+----2----+----

RED    ORANGE  YELLOW  GREEN

BLUE  INDIGO  PURPLE  VIOLET

CYAN  WHITE  FUCSIA  BLACK

GRAY  BROWN  PINK    MAGENTA


The following SAS program is submitted:

data WORK.COLORS;

      infile 'COLORS.TXT';

      input @1 Var1 $ @8 Var2 $ @;

      input @1 Var3 $ @8 Var4 $ @;

run;

What is the output of the program and why. kindly give the logic behind the program output.

Many thanks in advance

regards

Ved

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Yes, but in an earlier response you said " is that means in above code @ prevents second input statement to being read and results in repeatition of values of var1 and var2 for var3 and var4 ??".

My answer is no! Think about the two input statements.

     input @1 Var1 $ @8 Var2 $ @;

@1 will read Var1 from col1, then @8 will move the pointer to col8 and input Var2. Finally, the @ will cause the pointer to not move to the next record until either the next instruction or the next iteration of the data step

Then:

     input @1 Var3 $ @8 Var4 $ @;

will move the pointer back to col1 and read Var3, then the @8 will cause the pointer to input Var4 from col8. Finally, the @ will cause the pointer to not move to the next record until either the next instruction or the next iteration of the data step. There aren't any more instructions, thus it is irrelevant as the program will then move to the next iteration.

View solution in original post

9 REPLIES 9
ballardw
Super User

Did you try running the program and looking at the output?

The key is the last @ in the first input. Look at the documentation for INPUT.

SASFREAK
Obsidian | Level 7

Yes, i have runned the code and result it showing me the repeatition of the values of var1 and var2 for var3 and var4 respectively.As per 9.2 documentation @ : (trailing @) prevents SAS from automatically reading a new data record into the input buffer when a new INPUT statement is executed within the same iteration of the DATA step.

is that means in above code @ prevents second input statement to being read and results in repeatition of values of var1 and var2 for var3 and var4 ??

Please correct if i am wrong.

thanks in advance.

rgds

ved

art297
Opal | Level 21

You're wrong, other than your explanation of the trailing blank. Provide a different explanation of your code. Would help if you explain what the @1 does.

SASFREAK
Obsidian | Level 7

I think the first @ trailing in input statment hold the pointer and prevent reading the next input statement.

art297
Opal | Level 21

I don't disagree with your explanation of the first trailing @. I was suggesting that you explain what the @1 and @8 do, and why the second trailing @ is irrelevant.

SASFREAK
Obsidian | Level 7

i think @1 guide the pointer to read the first record(RED) for first variable i.e, Var1 and similarly @8 guide pointer to read second record(ORANGE )  for Var2.

art297
Opal | Level 21

Yes, but in an earlier response you said " is that means in above code @ prevents second input statement to being read and results in repeatition of values of var1 and var2 for var3 and var4 ??".

My answer is no! Think about the two input statements.

     input @1 Var1 $ @8 Var2 $ @;

@1 will read Var1 from col1, then @8 will move the pointer to col8 and input Var2. Finally, the @ will cause the pointer to not move to the next record until either the next instruction or the next iteration of the data step

Then:

     input @1 Var3 $ @8 Var4 $ @;

will move the pointer back to col1 and read Var3, then the @8 will cause the pointer to input Var4 from col8. Finally, the @ will cause the pointer to not move to the next record until either the next instruction or the next iteration of the data step. There aren't any more instructions, thus it is irrelevant as the program will then move to the next iteration.

SASFREAK
Obsidian | Level 7

I am sorry for the inconvience . I think i missinterpret your earlier suggestion, as i was talking about trailing(@) and ignored the explaination of @1 point.

Thanks for the correction and help me to understand the whole processing of the code. Your explaination helped me alot.

regards

Ved

art297
Opal | Level 21

And a 2nd key is to look where the second input statement is pointing. The @, at the end of the second input statement is superfluous, but doesn't hurt anything.

But run the code and carefully compare the input file with the file that the data step creates.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 3243 views
  • 3 likes
  • 3 in conversation