DATA Step, Macro, Functions and more

Reading Multiple Records per Observation

Reply
New Contributor
Posts: 2

Reading Multiple Records per Observation

Is anyone can help with this data set ??

I'm a starter on SAS.

Thank you in advance.

 

LEE ATHNOS
1215 RAINTREE CIRCLE
PHOENIX AZ 85044
HEIDIE BAKER
1751 DIEHL ROAD
VIENNA VA 22124
MYRON BARKER
131 DONERAIL DRIVE
ATLANTA GA 30363
JOYCE BENET
85 MAPAVENUE
MENLO PARK CA 94025

Respected Advisor
Posts: 4,173

Re: Reading Multiple Records per Observation

Reading data from multiple lines is not an issue as such. The challenge with the data you've posted is that we don't know what's on which line - so for example when does a new address start.

 

Does your data contain anything additional to what you've posted - eg. at the beginning of the line some sort of an indicator value telling us if the line contains the name, the street address and so on?

 

New Contributor
Posts: 2

Re: Reading Multiple Records per Observation

Thank you for the reply Patrick.

 

I believe the first line contains field of Fname and Lname;

               the second line contains field of Address (with street name and number) ;

               the third line contains field of city, state and zipcode.

 

I wrote the code as such : 

                                         data info;
                                          infile "/folders/myfolders/SAS Practice.txt";
                                          input #1    Fname:$6.   Lname :$6.
                                                   #2    Address : $21.
                                                   #3    City : $10.   State :$2.   Zipcode;
                                          run;

  But the result only showed  number on the address , and incomplete city.

 Could you tell me where it went wrong? Thanks.

 

Super User
Posts: 5,503

Re: Reading Multiple Records per Observation

Two issues (one small, one large).

 

First, remove the colons for ADDRESS and CITY.  That's what stops the INPUT statement when it hits a blank, rather than reading the full 21 (or the full 10) characters.

 

Second, the third line will be much more difficult to work with because some city names contain two words instead of one.  You could read the entire line as one long variable, then count the number of words in it to determine which piece(s) go into which variables.

Super User
Posts: 19,789

Re: Reading Multiple Records per Observation

Because a City can be 2 words, you won't be able to do a straight read for line 3. Unless you have two spaces between your other fields which is unlikely in the real world. If this is a class room example it may. 

 

You'll need to read the line in as one string and then parse it. Parse it out backwards, last field is zip, second last is state and the remaining is city. 

 

Are you sure you're using the Colon correctly here? I don't think you need it in all cases. It's used to read text with embedded blanks. Would that be true with First/Last Name? 

 

 

 

 

Super User
Posts: 19,789

Re: Reading Multiple Records per Observation

This gets things in, I'll leave the parsing of the final line to you.

 

data info;
informat fname lname $15.;
input #1    Fname $   Lname  $
	  #2    Address  $30.
	  #3    String3  $30.;
cards;
LEE ATHNOS
1215 RAINTREE CIRCLE
PHOENIX AZ 85044
HEIDIE BAKER
1751 DIEHL ROAD
VIENNA VA 22124
MYRON BARKER
131 DONERAIL DRIVE
ATLANTA GA 30363
JOYCE BENET
85 MAPAVENUE
MENLO PARK CA 94025
;
run;
Trusted Advisor
Posts: 1,560

Re: Reading Multiple Records per Observation

According to:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000146292.htm#a000...

 

the input statement can be written also as:

 

INPUT fname $10 lname $10 / address $30 / strung3 $30;

 

The / tells SAS to continue on next line.

Super User
Posts: 7,778

Re: Reading Multiple Records per Observation

Modify your data step like this:

data info;
infile "/folders/myfolders/SAS Practice.txt" truncover;
input Fname:$6. Lname :$6.;
input Address  $30.;
input city_state_zip $100.;
length city $30 state $2;
words = countw(city_state_zip);
zipcode = input(scan(city_state_zip,words),best.);
state = scan(city_state_zip,words-1); 
do i = 1 to words - 2;
  city = catx(' ', trim(city),scan(city_state_zip,i));
drop i;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 7 replies
  • 467 views
  • 4 likes
  • 6 in conversation