- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 08-12-2008 01:57 PM
(7749 views)
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Scott Barry
SBBWorks, Inc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
what is the need of cat fun. in this statement. explain?
call symput('ANTVAR',cat(countw(_infile_,'|')));
call symput('ANTVAR',cat(countw(_infile_,'|')));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
"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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Oooo - kewl! Never used SYMPUTX. Thanks much for the tip!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Scott Barry
SBBWorks, Inc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
/Linus
Data never sleeps
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
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