BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello All,
Here is the issue: I have a pipe delimited TXT file which has around 200 variables. Iam reading the file using INFILE statement in a DATA STEP, I want to read ALL the variables without specifying the variable names in the INPUT statement, do we have any option in SAS to do that?

PS: I dont want to use PROC IMPORT in this case due to some character constraints.


Thanks
Bob
10 REPLIES 10
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
One technique of several - use a DATA step and code an INPUT; statement (no variables listed). Then use the _INFILE_ variable within a DO/END loop to parse your individual fields with the SCAN function.

Scott Barry
SBBWorks, Inc.
LinusH
Tourmaline | Level 20
If you want to do something dynamic without individual coding for each variable, I think you first have to determine how many variables there are in the file, and us this information either to specify a array or to create a macro %DO loop when reading the file. Array example:

data dat;
infile '~/data.dat';
input;
call symput('ANTVAR',left(put(indexc(_infile_,'|') + 1,3.)));
stop;
run;

data dat;
infile '~/data.dat' dlm='|';
array dynvar(*) dynvar1 - dynvar&ANTVAR.;
input dynvar(*);
run;

Regards,
Linus
Data never sleeps
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Regarding the first DATA step in the previous post, I believe you want to use the SAS function (new with v9) COUNTW instead of INDEXC, such as:

data _null_;
infile datalines dsd dlm='|';
input;
call symput('ANTVAR',cat(countw(_infile_,'|')));
stop;
datalines;
1|2|3|4|5
run;
%put Survey Says? Input &antvar variables;


Regards,

Scott Barry
SBBWorks, Inc.
Pavan_SAS
SAS Employee
what is the need of cat fun. in this statement. explain?

call symput('ANTVAR',cat(countw(_infile_,'|')));
andreas_lds
Jade | Level 19
Just have a look at the online help:
"The CAT function removes leading and trailing blanks from numeric arguments after it formats the numeric value with the BEST. format."

I suggest using call symputx to create the macro variable, making the call of the cat function redundant.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Oooo - kewl! Never used SYMPUTX. Thanks much for the tip!
Pavan_SAS
SAS Employee
hi,
i am not getting proper output.

data is at : C:\Documents and Settings\pavan\Desktop\inf.txt

pavan*25*m
ramu*12*m
laxmi*78*f
rani*16*f

code is:

data dat;
infile 'C:\Documents and Settings\pavan\Desktop\inf.txt';
input;
call symput('ANTVAR',left(put(indexc(_infile_,'*')+1,3.)));
stop;
run;
%put &ANTVAR;

data dat;
infile 'C:\Documents and Settings\pavan\Desktop\inf.txt' dlm='*';
array dynvar(*) dynvar1 - dynvar&ANTVAR.;
input dynvar(*);
run;

i am getting 7 variables. instead of 3 variables.

help me ! Message was edited by: pavan
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Please read my most recent reply - the function INDEXC was suggested where it should be a different function to determine the number of variables.

Scott Barry
SBBWorks, Inc.
LinusH
Tourmaline | Level 20
Sorry, I don't what I was thinking of. I meant to suggest countc, but as Scott says counw is a bit neater (just make sure to remove the +1 part from my example).

/Linus
Data never sleeps
Peter_C
Rhodochrosite | Level 12
since this is just to investigate the content (like lining up the delimiters), I tend to type as little as possible:[pre] data ;
length col1-col300 $20 ;
infile "your input file" dsd lrecl=20000 ;
input col: ;
run ;[/pre]That provides a quick version of the data that i can examine.
My preferred viewer in these situations is FSView. It allows you to try data conversions using input() and other functions as well as data formatting ~ all dynamically, and with a facility to keep these interpretations until the next time;-)
With luck you won't be running SAS on E.G. where FSView is not possible.

Good luck

PeterC

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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