BookmarkSubscribeRSS Feed
1162
Calcite | Level 5
I have a tab delimited file with 116 variables in it. I only need the 13th and 71st variables for the project I'm doing.

Is there some way to skip 12 tabs and then read in a variable? I came up with this solution, but I'm hoping there is a better one.

data work.in;
infile "delimited.txt" dlm='09'X dsd missover;
input @'09'X @'09'X @'09'X @'09'X @'09'X @'09'X @'09'X @'09'X @'09'X @'09'X @'09'X @'09'X Identifier :$16.;
run;

I would have to put in 57 more @'09'Xs to read in my next variable. I don't want to use dummy variables that I later drop because the columns are a mix of character and numeric formats of varying lengths.

Thanks.
4 REPLIES 4
deleted_user
Not applicable
just thinking out loud, but you might try a do loop?

*this is pseudo code;
array var(116);
do i = 1 to 116;
input var(i) @;
if i in (13, 71);
end;
1162
Calcite | Level 5
Thanks. Your idea worked well for me after tweaking it a bit. Here's what I ended up with:

data work.a (drop=i cell);
infile "delimited.txt" dlm='09'X dsd missover lrecl=2048;
do i = 1 to 71;
input cell :$16. @;
if i = 13 then Identifier = cell;
if i = 71 then Value = input(cell, 8.);
end;
output;
run;

Still, I thought there was a way to move the pointer by a number of delimiters so that this could all be written as one input statement (no loops or if-then steps).
deleted_user
Not applicable
glad to see it worked out for you!

i think the only way to selectively read variables is if the file is fixed-width? otherwise, you need to read everything on the line up to the last variable you want to keep.
deleted_user
Not applicable
that approach uses a loop in the data step iteration over which you can improve, just a little.
The "input" statement allows some economy of syntax/processing by using variable lists, like in this example :[pre]
data reduced( keep= identifier value ) ;
length dum1-dum70 $1 ;
informat identifier $16. value 8.;
infile "delimited.txt" dsd dlm= '09'x truncover lrecl= 2048 ;
input dum1-dum12 identifier dum14-dum70 value ;
run;[/pre]

Happy New Year

PeterC

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 4 replies
  • 699 views
  • 0 likes
  • 2 in conversation