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.
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 $
;
Here is a clearer screen capture of the error of the data (how I am not skipping a line).
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 $
;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.