Hello, I am writing an infile data step that that reads .ascii and .txt files with repeating lines of code for records. However our population sample data files come with 20 character long header string specifying when and where it came from. How do I instruct SAS to begin writing the input statement after skipping the first 20 characters ONLY ONCE?
Sample Code:
data LIBNAME.POPULATION_INFILE;
infile FILELOC1;
input
@1 "SAMPLE-YEAR-MONTH"n 6.
@7 "SAMPLE-SOURCE"n 1.
@8 "COUNTY-CODE"n $2.
@10 "COUNTY-DISTRICT-NBR"n $3.
@13 "CITY NAME"n $40.;
run;
We need more details.
If the extra 20 characters are every record then just change your @ cursor movement commands to account for the new location.
If it is just on the first record that you need skip then change the input statement to NOT use specific columns. Looks like you don't need them anyway.
data LIBNAME.POPULATION_INFILE;
infile FILELOC1;
if _n_=1 then input +20 @;
input
"SAMPLE-YEAR-MONTH"n 6.
"SAMPLE-SOURCE"n 1.
"COUNTY-CODE"n $2.
"COUNTY-DISTRICT-NBR"n $3.
"CITY NAME"n $40.
;
run;
@Chiginsky wrote:
Hello, I am writing an infile data step that that reads .ascii and .txt files with repeating lines of code for records. However our population sample data files come with 20 character long header string specifying when and where it came from. How do I instruct SAS to begin writing the input statement after skipping the first 20 characters ONLY ONCE?
Sample Code:
data LIBNAME.POPULATION_INFILE;
infile FILELOC1;
input
@1 "SAMPLE-YEAR-MONTH"n 6.
@7 "SAMPLE-SOURCE"n 1.
@8 "COUNTY-CODE"n $2.
@10 "COUNTY-DISTRICT-NBR"n $3.
@13 "CITY NAME"n $40.;
run;
if the obnoxious string is the first ROW of the data then
infile FILELOC1 firstobs=2;
should work. That tells SAS to start reading on the second line of the data.
If that is not the case then show an actual example that looks like your data as there are several other ways and the actual content makes a difference. Copy text from the file including maybe 6 rows of data and paste into a code box opened with the forum's {I} icon to preserve actual text appearance as the main message windows reformat text.
You might consider NOT using name literals as the code gets cumbersome quickly. You can name your variables what you like such as COUNTY_DISTRICT_NBR or CityName. Assign a LABEL to get column headers that look "nicer" when needed.
label COUNTY_DISTRICT_NBR = 'County District Number'; for example
We need more details.
If the extra 20 characters are every record then just change your @ cursor movement commands to account for the new location.
If it is just on the first record that you need skip then change the input statement to NOT use specific columns. Looks like you don't need them anyway.
data LIBNAME.POPULATION_INFILE;
infile FILELOC1;
if _n_=1 then input +20 @;
input
"SAMPLE-YEAR-MONTH"n 6.
"SAMPLE-SOURCE"n 1.
"COUNTY-CODE"n $2.
"COUNTY-DISTRICT-NBR"n $3.
"CITY NAME"n $40.
;
run;
However our population sample data files come with 20 character long header string specifying when and where it came from. How do I instruct SAS to begin writing the input statement after skipping the first 20 characters ONLY ONCE?
So do you need anything from the first line? Can you use FIRSTOBS on the infile statement to specify start reading at the second line?
data LIBNAME.POPULATION_INFILE; infile FILELOC1 firstobs=2 truncover;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.