DATA Step, Macro, Functions and more

Basics

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Basics

---I----1 0---I----20---I----30 
 
Ruth 39 11 
 
Jose 32 22 
 
Sue 30 33 
 
John 40 44 
 
 
The following SAS program is submitted: 
 
 
data test; 
 
infile ‘employee’; 
 
input employee_name $ 1-4; 
 
if employee_name = ‘Ruth’ then input idnum 10-11; 
 
else input age 7-8; 
 
run; 
 
 
What value does the variable IDNUM contain when the name of the employee is “Ruth”? 
 
 
I have no clue how to deduct the answer.Please help me with reason.Answer for this question is appreciated.However, I need an explanation.thanks in advance.

Accepted Solutions
Solution
‎04-30-2017 01:23 PM
Super User
Posts: 6,941

Re: Basics

Run the step first. Then apply your knowledge about "data step concepts" (if this is not yet present, get it through googling for the phrase and study the respective SAS documentation) to the result, and you will arrive at the answer.

The intent of such exercises is that you find the answer (and thus hone your skills), not somebody else.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎04-30-2017 01:23 PM
Super User
Posts: 6,941

Re: Basics

Run the step first. Then apply your knowledge about "data step concepts" (if this is not yet present, get it through googling for the phrase and study the respective SAS documentation) to the result, and you will arrive at the answer.

The intent of such exercises is that you find the answer (and thus hone your skills), not somebody else.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 9,681

Re: Basics

Since you didn't add @ at the end of "input employee_name $ 1-4; ".

that means point will go to the next row.

it should be 22 .

Occasional Contributor
Posts: 12

Re: Basics

Thanks Mr.Ksharp. I am still novice to SAS and still in the learning curve.I couldn't run this program in SAS studio.I tried it.It showing the syntax error.That's why i posted in this forum.Will read the basics again.
SAS Super FREQ
Posts: 8,743

Re: Basics

Hi:

  To test the program, you either would have needed to make a file called "employee" and type the datalines into the file or modify the program slightly to use the INFILE DATALINES technique for reading in test data. Here's an example of using INFILE DATALINES:

data test; 
  infile datalines; 
  input employee_name $ 1-4; 
  if employee_name = 'Ruth' then input idnum 10-11; 
  else input age 7-8; 
datalines;
Ruth  39 11 
Jose  32 22 
Sue   30 33 
John  40 44 
; 
run; 
proc print data=test;
  title '1) After running original program';
run;

  We do show this technique in our Programming 1 class and the technique of using DATALINES to provide a program with "instream" data is well documented.

 

  Then, once you run the program and see that KSharp was correct, that the value would be 22 -- as shown below:

input_statement_behavior.png

--then you can read about using the single trailing @ sign to modify the behavior of the INPUT statement.

 

  Every time an INPUT statement is encountered in a program, (EVERY time), a new row or new dataline is loaded into the INPUT buffer area. So without any other controls in place, SAS will go to a new dataline after reading the name on the first row.

 

Cynthia

Occasional Contributor
Posts: 12

Re: Basics

Thanks  a lot for showing the program in detail .I did something when writing this program in SAS studio.I'll fix it and run.

PROC Star
Posts: 7,363

Re: Basics

I agree with @KurtBremser that you really should first experiement and see if you can answer the questions yourself.

 

If there really are blank lines between each record, the code won't even run. And, if there aren't blank lines between each record, and the data are exactly the way you entered them, then the answer is 2 as SAS will try to read two digits beginning in column 10, but only one digit exists there.

 

Art, CEO, AnalystFinder.com

 

SAS Super FREQ
Posts: 8,743

Re: Basics

Hi:
If this is an interview or homework question, then you have everything you need to run the program and test it to understand how the INFILE statement works when coded as you show.

cynthia
New Contributor
Posts: 3

Re: Basics

ans is 22. because when you put input statement again after then.. then it will take value of next variable.(as per my knowledge)

Super User
Posts: 10,500

Re: Basics

It is a very good idea to post code and example data using codeboxes opened with the forum {i} menu icon.

As posted your code won't produce very similar results as the forum window has likely inserted additional blank lines.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 291 views
  • 2 likes
  • 7 in conversation