Sample 24709: Read a file description to help generate code to read in a second file
Use a DATA step to read in a file that contains variable
names and LABEL information for a second file. Generate the
DATA step code to read the second file via CALL EXECUTE.
Note:
CALL EXECUTE resolves an argument and issues the resolved
value for execution. CALL EXECUTE is fully documented in
SAS Macro Language: Reference.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* This sample involves two files, DESCRIPTION.txt and DATA.txt. */
/* Read DESCRIPTION.txt to generate the DATA step code to read */
/* in DATA.txt. */
/* Create DESCRIPTION.TXT and DATA.TXT */
data _null_;
file 'c:\description.txt';
put
"Qb2_1" /
"T 123" /
"T AGE" /
"R" /
"Qb3_1" /
"T GENDER" /
"Qb4_1" /
"T EDUCATION" /
;
run;
data _null_;
file 'c:\data.txt';
put
"B2_1,B3_1,B4_1" /
"20,1,12" /
"13,0,7"
;
run;
/* Read DESCRIPTION.txt and generate code to read DATA.txt */
data _null_;
retain varname varlabel inputlist;
length inputlist $32767;
/* Read file description first */
infile 'c:\description.txt' end=finish truncover;
/* On first iteration of step reading the file description, */
/* start building the code to read the data file */
if _N_=1 then
call execute('data new;
infile "c:\data.txt" dsd truncover firstobs=2;');
input @1 test $1. @;
/* Records that start with a Q contain variable names, records that */
/* start with T may contain attribute information */
if test='Q' then do;
input varname :$8.;
inputlist=trim(inputlist)||' '||varname;
end;
else if test='T' then do;
input +1 test2 $1. @;
if test2 not in('1','2','3','4','5','6','7','8','9','0',' ') then do;
input @2 varlabel :$20.;
/* Build syntax for a label statement based upon info from T record */
labels='LABEL '||TRIM(varname)||'='||TRIM(varlabel)||";";
call execute(labels);
end;
else input; /* Release trailing @ */
end;
/* At end of file, code a step boundary */
if finish then do;
call execute('input'||trim(inputlist)||';');
call execute('run;');
end;
run;
proc print data=new label;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs AGE GENDER EDUCATION
1 20 1 12
2 13 0 7
Use a DATA step to read in a file that contains variable names and LABEL information for a second file. Generate the DATA step code to read the second file via CALL EXECUTE.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> CALL routines
|
Date Modified: | 2005-12-08 11:34:24 |
Date Created: | 2004-09-30 14:09:06 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |