BookmarkSubscribeRSS Feed
Rockyxiu
Calcite | Level 5

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

7 REPLIES 7
Patrick
Opal | Level 21

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?

 

Rockyxiu
Calcite | Level 5

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.

 

Astounding
PROC Star

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.

Reeza
Super User

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? 

 

 

 

 

Reeza
Super User

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

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.

Kurt_Bremser
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 1337 views
  • 4 likes
  • 6 in conversation