DATA Step, Macro, Functions and more

know the source of multiple infile files

Reply
Regular Contributor
Posts: 241

know the source of multiple infile files

[ Edited ]

Hello all,

 

I have more than one external files to be infiled into SAS,but

 

I want to know which record comes from which different one of the four files.

 

quarter1.dat
1 120321 1236 154669 211326
1 326264 1326 163354 312665
1 420698 1327 142336 422685
1 211368 1236 156327 655237
1 378596 1429 145678 366578

quarter2.dat
2 140362 1436 114641 362415
2 157956 1327 124869 345215
2 215547 1472 165578 412567
2 204782 1495 150479 364474
2 232571 1345 135467 332567

quarter3.dat
3 140357 1339 142693 205881
3 149964 1420 152367 223795
3 159852 1479 160001 254874
3 139957 1527 163567 263088
A 150047 1602 175561 277552

quarter4.dat
4 479574 1367 155997 36134
4 496207 1459 140396 35941
4 501156 1598 135489 39640
4 532982 1601 143269 38695
4 563222 1625 147889 39556
filename year ('d:\quarter1.dat' 'd:\quarter2.dat' 'd:\quarter3.dat' 'd:\quarter4.dat');
data temp;
infile year;
input quarter sales tax expenses payroll;
run;

 

That means I want there will be a new  flag variable can tell me "A 150047 1602 175561 277552" comes from quarter 3.(don't modify the external files)

 

 

 

I hope there was some code like this:

 

 filename year ('d:\quarter1.dat'(in=a) 'd:\quarter2.dat'(in=b) 'd:\quarter3.dat'(in=c) 'd:\quarter4.dat'(in=d));

data temp;
infile year;
input quarter sales tax expenses payroll;

if a then flag=1;

if b then flag=2;

if c then flag=3;

if d then flag=4;
run;

 

 

Thanks!

Super User
Posts: 19,878

Re: know the source of multiple infile files

Posted in reply to GeorgeSAS

The INFILE statement has a FILEVAR option that stores the filename. 

 

Infile .... Filevar=filevar;

 

source=FILEVAR;

 

*rest of SAS code...

Respected Advisor
Posts: 3,156

Re: know the source of multiple infile files

Posted in reply to GeorgeSAS

Don't think there is a way of doing it out of box upon your existing code, however, if you can put your file names in the cards/dataline/dataset, here is one way (not tested, may need tweak):

 

data want;
infile datalines;
   length fileloc $ 300;
   input fileloc $ ; /* read instream data       */
  /* The INFILE statement closes the current file 
     and opens a new one if FILELOC changes value 
     when INFILE executes                        */
   infile dummy filevar=fileloc 
          end=done; 
  /* DONE set to 1 when last input record read  */
   do while(not done);
  /* Read all input records from the currently  */
  /* opened input file, write to want       */
     input var1-var5;
     source=fileloc;
   output;
   end;
     
   datalines;
d:\quarter1.dat
d:\quarter2.dat
d:\quarter3.dat
d:\quarter4.dat
;

'Souce' field will contain the incoming file information.

 

Ask a Question
Discussion stats
  • 2 replies
  • 174 views
  • 0 likes
  • 3 in conversation