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

I am relatively new to SAS and as I was trying to read in a data set from school I came across an interesting problem: the txt file I was trying to import to SAS had an extra observation column and thus "shifted" my columns names to the left one spot of their original position. Is there an option or statement in proc import that allows me to ignore that column so the column names are aligned with their respective data columns?

%let myData=/home/u59821735/My Files/MyData;
proc import datafile="&myData/BodyFat.txt" 
			dbms=dlm
			out=body_fat
			replace;
	guessingrows=max;
run;

jdlax00_0-1641269267471.pngjdlax00_1-1641269299005.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Not that easy with proc import, post-processing is required to get a useful dataset. You have to ignore the first row containing the names of the variables, drop the first variable and use proc datasets to rename the variables. A better approach starts by dropping proc import and writing a data step. Have look at the log, the generated code is there, so you could copy it and adapt it to your needs.

 

Untested:

%let myData=/home/u59821735/My Files/MyData;
proc import datafile="&myData/BodyFat.txt" 
			dbms=dlm
			out=body_fat(drop= var1)
			replace;
	guessingrows=max;
        datarow=2;
        getnames=no;
run;

proc datasets library= work nolist;
  modify body_fat;
    rename var2 = Age var3 = Weight /* add other vars here */;
  run;
quit;

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

Not that easy with proc import, post-processing is required to get a useful dataset. You have to ignore the first row containing the names of the variables, drop the first variable and use proc datasets to rename the variables. A better approach starts by dropping proc import and writing a data step. Have look at the log, the generated code is there, so you could copy it and adapt it to your needs.

 

Untested:

%let myData=/home/u59821735/My Files/MyData;
proc import datafile="&myData/BodyFat.txt" 
			dbms=dlm
			out=body_fat(drop= var1)
			replace;
	guessingrows=max;
        datarow=2;
        getnames=no;
run;

proc datasets library= work nolist;
  modify body_fat;
    rename var2 = Age var3 = Weight /* add other vars here */;
  run;
quit;
jdlax00
Calcite | Level 5
Thank you so much! It worked like a charm.
Tom
Super User Tom
Super User

Why use PROC IMPORT to read a text file?

Just write the data step yourself. You will have much more control (and in this case less code).

%let myData=/home/u59821735/My Files/MyData;
data body_fat ;
  infile "&myData/BodyFat.txt" dsd dlm=' ' firstobs=2 truncover;
  input row brozek age weight height neck chest adbom;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1379 views
  • 1 like
  • 3 in conversation