BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jcox22
Calcite | Level 5

I'm a new SAS programmer and am trying to figure out how to read in column headers for a data frame from a text file. The problem is the data look like this:

Var1 Var2 Var3

Var4 Var5 Var6

1 data for 1

data for 1

2 data for 2

data for 2

...

And My output looks like this:

obs Var1 Var2 Var3

1      Var4 Var5 Var6

2    1 data for 1 

...

This is what I have so far any feedback I appreciate.

 

PROC IMPORT DATAFILE= "/folders/myfolders/file.txt" out = WORK.MYOUTPUT DBMS= TAB replace;
getnames = yes;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
When your data isn't a nice clean formatted file you cannot use PROC IMPORT. You'll need to tell SAS how to read the data using a data step by specifying the layout.

The documentation goes through a couple of examples in detail if you're aiming to understand what's going on:
https://documentation.sas.com/?docsetId=basess&docsetTarget=p0s16wvzu0z9q7n0zmxia30s6qyc.htm&docsetV...

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Is your file organized so that every observation takes two lines?  Or is just the "header" split into two lines?

Is the split at the same place for every observation (and for the headers?).

 

You probably will want to skip asking SAS to guess how to read the file and just tell it explicitly how to read it.

data want;
  infile  "/folders/myfolders/file.txt" dsd truncover firstobs=3;
  input var1 var2 var3 / var4 var5 var6;
run;

 

For more detailed answer show some actual example (or a representative example) of the lines in the file.  Don't paste the lines into the test of your message. Instead use the Insert Code button so you get a pop-up window where you can past text so it will be displayed using fixed width font and it will not be flowed into paragraphs.

Reeza
Super User
When your data isn't a nice clean formatted file you cannot use PROC IMPORT. You'll need to tell SAS how to read the data using a data step by specifying the layout.

The documentation goes through a couple of examples in detail if you're aiming to understand what's going on:
https://documentation.sas.com/?docsetId=basess&docsetTarget=p0s16wvzu0z9q7n0zmxia30s6qyc.htm&docsetV...

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 1551 views
  • 2 likes
  • 3 in conversation