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;
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;
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;
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: