Reading Raw Data over two lines using a conditional IF statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Reading Raw Data over two lines using a conditional IF statement

[ Edited ]

Hello again,

I am teaching myself SAS through working through some books, right now I am stuck on a question from The Little SAS Book Exercises and Projects.

 

I can't work out why my below code is not working, I realise this is probably a simple question but I have spent the last hour and a half trying to work this out for myself and can't make progress so I am turning once again tot he SAS community for help.

 

What I am trying to do is read in raw data that each observation is over two lines of the raw data file. I did this fine but the next part of my requirement is to only read in specific data, in this case when the Classification_Group variable is not equal to 'student'.

 

My problem seems to be that I can successfully use the IF.... THEN ....DELETE format but I can't seem to work out how to tell SAS to skip a line when deleteing a line from the input statement. It seems that SAS deletes the input but goes to the very next line rather than doing what I want it to do and skip the rest of the ovservation.

 

It is probably best I use an example, below is a little cut and paste from my data file:

35    Faculty  ROBIN  MURRAY
      email:robin.murray@csu.edu  phone:555-4389  PHYS
36    Student  DAWN  CUNNINGHAM
      email:dawn.cunningham@csu.edu  phone:555-8747  MATH
37    Faculty  JAMIE  BRADLEY
      email:jamie.bradley@csu.edu  phone:555-8784  MCRO

 

What I want to do is read in observation 35 as it has the value Faculty but skip observation 36 as it has the value of Student but read atain observation 37.

 

Here is my code.

Data ComputerUsers;
	INFILE '/folders/myfolders/TheLittleSASBook/CodePractice/Chapter02/Data/CompUsers.txt'
		OBS = 80
		;
	INPUT 	User_Id Classification_Group $ @;
				IF Classification_Group = 'Student' THEN DELETE;
				INPUT	First_Name :$20.
						Last_Name :$20.
						#2 @'email:' Email_Address :$35.
						@'phone:' Campus_Phone_Number :$15.
						Department $;
				
PROC PRINT DATA = computerusers;
RUN;

I don't actually get any errors with my code, or any warnings but there are some comments that raise alarm bells. 

 
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
NOTE: SAS went to a new line when INPUT @'CHARACTER_STRING' scanned past the end of a line.
 
I attach my raw data file.
 
Below is a screen capture of how my data goes wrong.
ErrorOnSkipLine.JPG
 
There is a hint provided for the question as well:
Hint: For the rows of data for students, think about how to tell SAS to skip a line and remove the observation
 
I can't seem how to do both 'skip a line' and remove the observation, I tried using multiple if statements but could not get it to work.
 
And finally I am using SAS University Edition on a Windows 10 Machine.
 
Once again many thanks in advance for any assistance anyone can give me.
 
Pete

Accepted Solutions
Solution
‎05-01-2017 01:55 PM
Super User
Super User
Posts: 6,842

Re: Reading Raw Data over two lines using a conditional IF statement

Your first INPUT statement only read one line, but there are two lines per observation.

You could try adding the #2 tag to both of your INPUT statements .

INPUT #1 User_Id Classification_Group $ #2 @;
...
INPUT #1 First_Name :$20. Last_Name :$20.
      #2 @'email:' Email_Address :$35. 
         @'phone:' Campus_Phone_Number :$15. Department $
;

View solution in original post


All Replies
Occasional Contributor
Posts: 6

Re: Reading Raw Data over two lines using a conditional IF statement

Here is a clearer screen capture of the error of the data (how I am not skipping a line).

ErrorOnSkipLine.JPG

Solution
‎05-01-2017 01:55 PM
Super User
Super User
Posts: 6,842

Re: Reading Raw Data over two lines using a conditional IF statement

Your first INPUT statement only read one line, but there are two lines per observation.

You could try adding the #2 tag to both of your INPUT statements .

INPUT #1 User_Id Classification_Group $ #2 @;
...
INPUT #1 First_Name :$20. Last_Name :$20.
      #2 @'email:' Email_Address :$35. 
         @'phone:' Campus_Phone_Number :$15. Department $
;
Trusted Advisor
Posts: 1,459

Re: Reading Raw Data over two lines using a conditional IF statement

You can also make a slight change to your code:

	INPUT 	User_Id Classification_Group $ @;
		IF Classification_Group = 'Student' THEN DO;
                      DELETE; input ;  delete;  
                END; else do;
		     INPUT	First_Name :$20.
				Last_Name :$20.
			#2 @'email:' Email_Address :$35.
				@'phone:' Campus_Phone_Number :$15.
				Department $;
               end;
Occasional Contributor
Posts: 6

Re: Reading Raw Data over two lines using a conditional IF statement

Thanks Tom,
I would say "I can't believe it was that easy" but I actually knew it was going to be something as simple as that. I just couldn't figure it out for myself and spent quite some time trying.

The solution from Shmuel below is also quite interesting, more complex but interesting as well. (Thanks to Shmuel too).
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 252 views
  • 3 likes
  • 3 in conversation