1
votes
data &state.&sheet.;
            set di;
            retain &header.;
            infile in filevar= path end=done missover;  
            do until(done); 
                if _N_ =1 then
                    input  &headerlength.;      
                input &allvar.;
                output; 
            end;run;

variable path is in di data set.

I wanna read multiple txt files into one SAS data set. In each txt file the first row is header and I want to retain this header for each observation so I used if _N_ = 1 input header then input second row of other variables for analysis.

The output is very strange. only the first row contains header and other rows are not correct observations.

Could someone help me a little bit? Thank you so much.

3
_N_ is just the counter for the datastep, so it only takes the value of 1 once. - mjsqu
@mjsqu ...unless you reset it! - user667489
@mjsqu Thank you so much - sssss_1
No worries, it probably didn't need a full answer, just a nudge in the right direction! - mjsqu

3 Answers

0
votes

Try:

data &state.&sheet.;
     set di;
     retain &header.;
     infile in filevar= path end=done missover dlm='09'x; 
     input  &headerlength.; 
     do until(done); 
        input &allvar.;
        output; 
     end;
run;               
0
votes

I like Shenglin Chen's answer, but here's another option: reset the row counter to 1 each time the data step starts importing a new file.

data &state.&sheet.;
            set di;
            retain &header.;
            infile in filevar= path end=done missover;  
            do _N_ = 1 by 1 until(done); 
                if _N_ = 1 then input &headerlength.;      
                input &allvar.;
                output; 
            end;
run;

This generalises more easily in case you ever want to do something different with every nth row within each file.

0
votes

You should use WHILE (NOT DONE) instead of UNTIL (DONE) to prevent reading past the end of the file, and stopping the data step, when the file is empty. Or for some of the answers when the file only has the header row.