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

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
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
Pete_Murphy
Fluorite | Level 6

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

ErrorOnSkipLine.JPG

Tom
Super User Tom
Super User

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 $
;
Shmuel
Garnet | Level 18

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;
Pete_Murphy
Fluorite | Level 6
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).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 4 replies
  • 2575 views
  • 3 likes
  • 3 in conversation