BookmarkSubscribeRSS Feed
kbanders
Calcite | Level 5

hello!

I am trying to import a txt file, but I am having difficulty because I do not understand the delimiter in the file.I originally thought it was space delimited (code below), but the variables and columns do not match up. I have attached a sample of the data and data dictionary below. The end result should be 50 variables, not all will have values. Any assistance in making the variables and columns matched up would be much appreciated!

 

proc import OUT=mydata

datafile= "\\computer\data\data sample.txt"

DBMS=dlm REPLACE;

run;

 

5 REPLIES 5
mkeintz
PROC Star

And because, as @Reeza noted, you have a fixed width text file, you have a very easy program to write in a data step.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

@mkeintz wrote:

And because, as @Reeza noted, you have a fixed width text file, you have a very easy program to write in a data step.


If the dictionary has the correct columns.

Sajid01
Meteorite | Level 14

Hello @kbanders 
You have simple reading to be done.

Unfortunately I see error in your dictionary file.
I have created a proof of concept code you can use it to create your code which you may use at basis. Make changes as you may need. I am attaching my data file too.

data test;
Length Mem_id $10 Fam_mem_id $2 Claim_numbr $15;
infile "/Path_to_your_file/sample.txt"  truncover;
input @1 Mem_id $ @12 Fam_mem_id $ @14 Claim_numbr $;
run;

My sample data is attached.

 

Patrick
Opal | Level 21

@kbanders 

Should you still have problems importing your data after reading through the docu links shared on how to impute fixed length records then please post a data dictionary and data that matches. 

Also post the data step code you've already developed and can't make fully working. If there is a header section in your data then use option FIRSTOBS=<some number> on the Infile statement to only start imputing the data portion of your .txt file.

 

If you look at the Excel you've shared then:

- Tab Header Record indicates that there is a header record in your data. The txt file you've shared doesn't have such a header

- Tab Summary shows #REF instead of the start & end positions of the data (I could fix this issue)

 

And "worst" of all: In your .txt file the data in your first "column" is a string of 10 digits. This doesn't match your data dictionary where the first variable MBRID goes from position 1 to 30.

Patrick_0-1622261598240.png

 

If you need further help then please post your data dictionary as .csv because many people won't download and open .xlsx

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 1578 views
  • 0 likes
  • 6 in conversation