DATA Step, Macro, Functions and more

Reading All variables through INPUT statement

Reply
N/A
Posts: 0

Reading All variables through INPUT statement

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Reading All variables through INPUT statement

Posted in reply to deleted_user
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.
Super User
Posts: 5,424

Re: Reading All variables through INPUT statement

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

Re: Reading All variables through INPUT statement

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.
SAS Employee
Posts: 73

Re: Reading All variables through INPUT statement

what is the need of cat fun. in this statement. explain?

call symput('ANTVAR',cat(countw(_infile_,'|')));
Super Contributor
Posts: 345

Re: Reading All variables through INPUT statement

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

Re: Reading All variables through INPUT statement

Posted in reply to andreas_lds
Oooo - kewl! Never used SYMPUTX. Thanks much for the tip!
SAS Employee
Posts: 73

Re: Reading All variables through INPUT statement

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
Super Contributor
Super Contributor
Posts: 3,174

Re: Reading All variables through INPUT statement

Posted in reply to Pavan_SAS
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.
Super User
Posts: 5,424

Re: Reading All variables through INPUT statement

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
Valued Guide
Posts: 2,177

Re: Reading All variables through INPUT statement

Posted in reply to deleted_user
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
Ask a Question
Discussion stats
  • 10 replies
  • 285 views
  • 0 likes
  • 6 in conversation