Hi all,
Could any one explain the follwing program,what will be the result of this program and how we get that(the internal process how it reads the data).
Thanks.
data work.TEST;
drop City;
infile datalines;
input
@1 Name $14. /
@1 Address $14. /
@1 City $12. ;
if City="New York" then input @1 State $2.;
else input;
datalines;
Joe Conley
123 Main St
Janesville
WI
Jane Ngyuen
555 Alpha Ave
New York
NY
Jennifer Jason
666 Mt Diablo
Eureka
CA
;
This is an example of using Line-Pointer Control to read multiple records like your code, to create a Single Observation containing variables Name , Address, City , State ideally. Since you requested a drop for city , you will have only three variables - Name , Address and State
This / reads only one dataline field as a new record, each time, into the input buffer.
So it reads Name first then reads next Address record still considered as the same observation followed by City and then State.
Now my question is do you have spaces before and after each record in the dataline? Whatever be the case, I have illustarted an example showing the possible cases.
I assume Case 1 is what you are looking for.
Case 1: Without Spaces (recommended)
It will read Name(Joe Coneley) first. Since there is a / after name ,it reads next record Address(123 Main St). The / after Address will read the next record City (Janesville). Since there is a drop for City , it will dropped in the dataset. Since you have another input statement in the IF-THEN condition , it is same as a / and will read State. it will not give a value for State since the City requested is New York . But your first observation has City = Janesville. Hence no value for State.
Your second observation has City=New York , hence State has a value of NY.
Without spaces and drop City | Without spaces and keep City | ||||||||
Obs | Name | Address | State | Obs | Name | Address | City | State | |
1 | Joe Conley | 123 Main St | 1 | Joe Conley | 123 Main St | Janesville | |||
2 | Jane Ngyuen | 555 Alpha Ave | NY | 2 | Jane Ngyuen | 555 Alpha Ave | New York | NY | |
3 | Jennifer Jason | 666 Mt Diablo | 3 | Jennifer Jason | 666 Mt Diablo | Eureka |
Case 2: With spaces ( not the result you want)
It will read first record which is a blank, hence Name is a blank. Since there is a / after name ,it reads next record Address but the records shows value (Joe Coneley). The / after Address will read the next record City which is a blank. Since there is a drop for City , it will dropped in the dataset. Since you have another input statement in the IF-THEN condition , it is same as a / and will read State. it will not give a value for state since the City requested is New York . But your first observation has a blank value for City and does not satisfy the IF condition. Hence no value for State. this gives 1st observation
Follow this order for observation 2 and 3.
With spaces and drop City | With spaces and keep City | ||||||||
Obs | Name | Address | State | Obs | Name | Address | City | State | |
1 | Joe Conley | 1 | Joe Conley | ||||||
2 | Janesville | 2 | Janesville | ||||||
3 | Jane Ngyuen | 3 | Jane Ngyuen | ||||||
4 | New York | 4 | New York | ||||||
5 | Jennifer Jason | 5 | Jennifer Jason | ||||||
6 | Eureka | 6 | Eureka |
You can also refer:
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/viewer.htm#a002001051.htm
Hope this helps!
The result is easy: Run the code then Proc print;run;
I think if you look at that then the code might make sense.
You didn't get me i think,I know the answer,i just want to know how it reads data the way of execution.
This is an example of using Line-Pointer Control to read multiple records like your code, to create a Single Observation containing variables Name , Address, City , State ideally. Since you requested a drop for city , you will have only three variables - Name , Address and State
This / reads only one dataline field as a new record, each time, into the input buffer.
So it reads Name first then reads next Address record still considered as the same observation followed by City and then State.
Now my question is do you have spaces before and after each record in the dataline? Whatever be the case, I have illustarted an example showing the possible cases.
I assume Case 1 is what you are looking for.
Case 1: Without Spaces (recommended)
It will read Name(Joe Coneley) first. Since there is a / after name ,it reads next record Address(123 Main St). The / after Address will read the next record City (Janesville). Since there is a drop for City , it will dropped in the dataset. Since you have another input statement in the IF-THEN condition , it is same as a / and will read State. it will not give a value for State since the City requested is New York . But your first observation has City = Janesville. Hence no value for State.
Your second observation has City=New York , hence State has a value of NY.
Without spaces and drop City | Without spaces and keep City | ||||||||
Obs | Name | Address | State | Obs | Name | Address | City | State | |
1 | Joe Conley | 123 Main St | 1 | Joe Conley | 123 Main St | Janesville | |||
2 | Jane Ngyuen | 555 Alpha Ave | NY | 2 | Jane Ngyuen | 555 Alpha Ave | New York | NY | |
3 | Jennifer Jason | 666 Mt Diablo | 3 | Jennifer Jason | 666 Mt Diablo | Eureka |
Case 2: With spaces ( not the result you want)
It will read first record which is a blank, hence Name is a blank. Since there is a / after name ,it reads next record Address but the records shows value (Joe Coneley). The / after Address will read the next record City which is a blank. Since there is a drop for City , it will dropped in the dataset. Since you have another input statement in the IF-THEN condition , it is same as a / and will read State. it will not give a value for state since the City requested is New York . But your first observation has a blank value for City and does not satisfy the IF condition. Hence no value for State. this gives 1st observation
Follow this order for observation 2 and 3.
With spaces and drop City | With spaces and keep City | ||||||||
Obs | Name | Address | State | Obs | Name | Address | City | State | |
1 | Joe Conley | 1 | Joe Conley | ||||||
2 | Janesville | 2 | Janesville | ||||||
3 | Jane Ngyuen | 3 | Jane Ngyuen | ||||||
4 | New York | 4 | New York | ||||||
5 | Jennifer Jason | 5 | Jennifer Jason | ||||||
6 | Eureka | 6 | Eureka |
You can also refer:
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/viewer.htm#a002001051.htm
Hope this helps!
Thank you .
The short answer is:
1) Because of the use of two / symbols in the first INPUT statement it will read in three lines.
2) The fourth line will either be read as the STATE or else it will be ignored.
Other notes:
The @1 pointer controls are not needed as by default the pointer will start in column 1.
We do not need to worry about SAS reading past the end of the line when we are using CARDS (or "datalines") because SAS will pad the input to card images (80 column records).
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.
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.