BookmarkSubscribeRSS Feed
huikeng_sas
SAS Employee
Hi,

Is it possible to read a column in a csv file more than once using the list input statement?

Thanks!
6 REPLIES 6
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
For whatever purpose, you might consider resetting the input offset pointer to the start of the record and re-read the fields again with alternate variable names.

Scott Barry
SBBWorks, Inc.

1 data _null_;
2 infile datalines dsd dlm=',';
3 length b x_b $20;
4 input @;
5 input a $ b $ @1 @;
6 input x_a $ x_b $ @;
7 input c $;
8 putlog _all_;
9 list;
10 datalines;

b=y12345678 x_b=y12345678 a=x x_a=x c=z _ERROR_=0 _N_=1
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+---
11 x,y12345678,z
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


12 run
huikeng_sas
SAS Employee
Thanks Scotts for your prompt posting.

However, if I have a csv file that contain 50 columns and I need to read the last 2 columns twice, is there a more efficient way than resetting the pointer to column 1?

Thanks!
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
None I am aware of --- so, explain why this is necessary and maybe the forum subscribers can provide an alternative? For example, input the last two columns using $CHAR and then use the INPUT function to convert the data-string to another format using a suitable INFORMAT with the INPUT function.

Scott Barry
SBBWorks, Inc.
huikeng_sas
SAS Employee
That could be a possible workaround....thanks!
data_null__
Jade | Level 19
I don't see any advantage to reading a field twice versus using the input or other functions to further process fields already read with "regular" delimited input. But here is how you do it. The COLUMN= infile statement option allows you to remember where the column pointer is.

In the program you can use COLUMN directly in the second input statement if you want too.

[pre]
data have;
infile datalines dlm='|' dsd column=column;
input (a b)($) @;
col = column;
input (c1-c10) (@col $) d;
list;
datalines;
a1|b_1|c1|100
a2||c2|33
a2||c2|22
;;;;
run;
proc print;
run;
[/pre]
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Nice solution!

Scott

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 6 replies
  • 1411 views
  • 0 likes
  • 3 in conversation