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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 1053 views
  • 0 likes
  • 3 in conversation