DATA Step, Macro, Functions and more

Reading CSV file

Reply
SAS Employee
Posts: 18

Reading CSV file

Hi,

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

Thanks!
Super Contributor
Super Contributor
Posts: 3,174

Re: Reading CSV file

Posted in reply to huikeng_sas
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
SAS Employee
Posts: 18

Re: Reading CSV file

Posted in reply to huikeng_sas
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!
Super Contributor
Super Contributor
Posts: 3,174

Re: Reading CSV file

Posted in reply to huikeng_sas
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.
SAS Employee
Posts: 18

Re: Reading CSV file

That could be a possible workaround....thanks!
Respected Advisor
Posts: 3,799

Re: Reading CSV file

Posted in reply to huikeng_sas
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]
Super Contributor
Super Contributor
Posts: 3,174

Re: Reading CSV file

Posted in reply to data_null__
Nice solution!

Scott
Ask a Question
Discussion stats
  • 6 replies
  • 211 views
  • 0 likes
  • 3 in conversation