The SAS Output Delivery System and reporting techniques

More than one input statements

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

More than one input statements

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

;


Accepted Solutions
Solution
‎05-30-2013 01:41 PM
Occasional Contributor
Posts: 9

Re: More than one input statements

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
ObsNameAddressState ObsNameAddressCityState
1Joe Conley123 Main St 1Joe Conley123 Main StJanesville
2Jane Ngyuen555 Alpha AveNY 2Jane Ngyuen555 Alpha AveNew YorkNY
3Jennifer Jason666 Mt Diablo 3Jennifer Jason666 Mt DiabloEureka

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
ObsNameAddressState ObsNameAddressCityState
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!

View solution in original post


All Replies
Grand Advisor
Posts: 9,748

Re: More than one input statements

The result is easy: Run the code then Proc print;run;

I think if you look at that then the code might make sense.

Contributor
Posts: 70

Re: More than one input statements

You didn't get me i think,I know the answer,i just want to know how it reads data the way of execution.

Solution
‎05-30-2013 01:41 PM
Occasional Contributor
Posts: 9

Re: More than one input statements

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
ObsNameAddressState ObsNameAddressCityState
1Joe Conley123 Main St 1Joe Conley123 Main StJanesville
2Jane Ngyuen555 Alpha AveNY 2Jane Ngyuen555 Alpha AveNew YorkNY
3Jennifer Jason666 Mt Diablo 3Jennifer Jason666 Mt DiabloEureka

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
ObsNameAddressState ObsNameAddressCityState
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!

Contributor
Posts: 70

Re: More than one input statements

Thank you .

Super User
Super User
Posts: 5,975

Re: More than one input statements

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).

Post a Question
Discussion Stats
  • 5 replies
  • 1112 views
  • 0 likes
  • 4 in conversation