DATA Step, Macro, Functions and more

SAS macro to read a text file

Reply
New Contributor
Posts: 3

SAS macro to read a text file

Hi

 

I've written a macro to generate some code to read a text file.    The code generates the relevant read statements fine and I can copy the log and run it and it executes.

 

However when I run the actual macro it doesn't read the file.  It appears to thing its like some text. 

 

Can anyone help?

 

I've pasted the code and the log below.

 

Rob

 

 

The code is

%macro InputSetupData (CIRTASIS=,  InputFile=, OutputData=, Deal= , paramtab= );

%getinfile(&paramfile, &paramtab)  ;

%assignmacros(&paramtab);

%let commandString=%nrstr(Smiley Wink;
 
   DATA &OutputData.;

  %put INFILE "&InputFile" DELIMITER = '|' MISSOVER SCANOVER DSD LRECL=32767 /*FIRSTOBS=2*/;
  
          %put &commandString ;

    %do i=1 %to &nobs;

       %put INFORMAT  &&field&i &&informat&i;

    %end;

      
    %do i=1 %to &nobs;

       %put FORMAT  &&field&i &&format&i;

    %end;


    %do i=1 %to &nobs;

       %if &i=1 %then %put Input;
                %put &&field&i  &&input&i ;

    %end;

    %put &commandString ; 

        run;

%mend;


%InputSetupData(CIRTASIS=CIRT,  InputFile=&CIRT_Input, OutputData=CIRTData, Deal= , paramtab=Input_Fields_CS_CIRT_Setup_Data);

the log is

INFILE "\\Essentsvr\SAAS\Exhibits\CIRT\Surveillance\All-CIRT-Deals_012018_1.csv" DELIMITER = '|'
MISSOVER SCANOVER DSD LRECL=32767
;
INFORMAT  PoolID       $16.;
INFORMAT  LoanID       $14.;
INFORMAT  AsOfDate      $6.;
INFORMAT  Channel      $1.;
INFORMAT  Seller       $50.;
INFORMAT  Servicer       $50.;
INFORMAT  MasterServicer     $10.;
INFORMAT  ORate       Best32.;
INFORMAT  CRate        Best32.;
INFORMAT  OrigUPB      Best32.;
INFORMAT  IssUPB        Best32.;
INFORMAT  CurrentUPB      Best32.;
INFORMAT  Term       3.;
INFORMAT  OrigDate      $6.;
INFORMAT  FPDate       $6.;
INFORMAT  Age       3.;
INFORMAT  RMLM       3.;
INFORMAT  RMM        3.;
INFORMAT  Maturity      $6.;
INFORMAT  oLTV       3.;
INFORMAT  oCLTV       3.;
INFORMAT  Borrowers      2.;
INFORMAT  DTI       Best32.;
INFORMAT  FICO_Borr      3.;
INFORMAT  FICO_CoBorr     3.;
INFORMAT  FTBuyer      $1.;
INFORMAT  Purp       $10.;
INFORMAT  Prop       $10.;
INFORMAT  Units       1.;
INFORMAT  Occ       $9.;
INFORMAT  State       $2.;
INFORMAT  MSAcode      5.;
INFORMAT  Zip       3.;
INFORMAT  MI_Cov       3.;
INFORMAT  Prod       $3.;
INFORMAT  Prep_Prem      $1.;
INFORMAT  IO_Flag      $1.;
INFORMAT  IO_FPDate      $6.;
INFORMAT  IO_MthsToAmort     3.;
INFORMAT  DlqStatusCode     $2.;
INFORMAT  PayHist       $48.;
INFORMAT  modflag       $1.;
INFORMAT  MICancel_Flag      $1.;
INFORMAT  zerocode       $3.;
INFORMAT  zerodate      $6.;
INFORMAT  UPBRemoval      Best32.;
INFORMAT  RepurchaseDate     $6.;
INFORMAT  SchedPrin      Best32.;
INFORMAT  TotalPrin      Best32.;
INFORMAT  UnSchedPrin     Best32.;
INFORMAT  LPIDate      MMDDYY10.;
INFORMAT  ForeclosureDate    MMDDYY10.;
INFORMAT  DispositionDate    MMDDYY10.;
INFORMAT  ForeclosureCost     Best32.;
INFORMAT  MainPres       Best32.;
INFORMAT  RecoveryCost      Best32.;
INFORMAT  MiscExp       Best32.;
INFORMAT  TaxInsu       Best32.;
INFORMAT  NetSalPro       Best32.;
INFORMAT  MICredit       Best32.;
INFORMAT  RepMakeWholPro     Best32.;
INFORMAT  ForeclosureProceed    Best32.;
INFORMAT  NonIntUPB      Best32.;
INFORMAT  PrinForgiv      Best32.;
INFORMAT  OriginalListDate    MMDDYY10.;
INFORMAT  OriginalListPrice    Best32.;
INFORMAT  CurrentListDate    MMDDYY10.;
INFORMAT  CurrentListPrice    Best32.;
INFORMAT  FICO_Borr_Iss     3.;
INFORMAT  FICO_CoBorr_Iss    3.;
INFORMAT  FICO_Borr_Curr     3.;
INFORMAT  FICO_CoBorr_Curr    3.;
INFORMAT  MI_Type      $1.;
INFORMAT  ServicingActivity_Flag   $1.;
INFORMAT  CurrModLoss     Best32.;
INFORMAT  CumModLoss      Best32.;
INFORMAT  CurrCreditEventLoss   Best32.;
INFORMAT  CumCreditEventLoss    Best32.;
INFORMAT  Homeready      $1.;
INFORMAT  ForPrinWriteOff    Best32.;
INFORMAT  Relocation      $1.;
INFORMAT  ZeroChangeDate     $6.;
INFORMAT  Holdback      $1.;
INFORMAT  HoldbackDate     $6.;
INFORMAT  DelqInterest     Best32.;
INFORMAT  PropInspWaiver     $1.;
INFORMAT  HighBalLoan     $1.;
FORMAT  PoolID       $16.;
FORMAT  LoanID       $14.   ;
FORMAT  AsOfDate      $6.  ;
FORMAT  Channel      $1.  ;
FORMAT  Seller       $50.  ;
FORMAT  Servicer       $50.   ;
FORMAT  MasterServicer     $10.   ;
FORMAT  ORate       Best32.  ;
FORMAT  CRate        Best32.  ;
FORMAT  OrigUPB      Best32. ;
FORMAT  IssUPB        Best32. ;
FORMAT  CurrentUPB      Best32. ;
FORMAT  Term       3.   ;
FORMAT  OrigDate      $6.  ;
FORMAT  FPDate       $6.  ;
FORMAT  Age       3.   ;
FORMAT  RMLM       3.   ;
FORMAT  RMM        3.   ;
FORMAT  Maturity      $6.  ;
FORMAT  oLTV       3.   ;
FORMAT  oCLTV       3.   ;
FORMAT  Borrowers      2.   ;
FORMAT  DTI       Best32. ;
FORMAT  FICO_Borr      3.   ;
FORMAT  FICO_CoBorr     3.   ;
FORMAT  FTBuyer      $1.  ;
FORMAT  Purp       $10.  ;
FORMAT  Prop       $10.  ;
FORMAT  Units       1.   ;
FORMAT  Occ       $9.  ;
FORMAT  State       $2.  ;
FORMAT  MSAcode      5.   ;
FORMAT  Zip       3.   ;
FORMAT  MI_Cov       3.   ;
FORMAT  Prod       $3.  ;
FORMAT  Prep_Prem      $1.  ;
FORMAT  IO_Flag      $1.  ;
FORMAT  IO_FPDate      $6.  ;
FORMAT  IO_MthsToAmort     3.   ;
FORMAT  DlqStatusCode     $2.  ;
FORMAT  PayHist       $48.  ;
FORMAT  modflag       $1.  ;
FORMAT  MICancel_Flag      $1.  ;
FORMAT  zerocode       $3.  ;
FORMAT  zerodate      $6.  ;
FORMAT  UPBRemoval      Best32. ;
FORMAT  RepurchaseDate     $6.  ;
FORMAT  SchedPrin      Best32. ;
FORMAT  TotalPrin      Best32. ;
FORMAT  UnSchedPrin     Best32. ;
FORMAT  LPIDate      MMDDYY10. ;
FORMAT  ForeclosureDate    MMDDYY10. ;
FORMAT  DispositionDate    MMDDYY10. ;
FORMAT  ForeclosureCost     Best32. ;
FORMAT  MainPres       Best32. ;
FORMAT  RecoveryCost      Best32. ;
FORMAT  MiscExp       Best32. ;
FORMAT  TaxInsu       Best32. ;
FORMAT  NetSalPro       Best32. ;
FORMAT  MICredit       Best32. ;
FORMAT  RepMakeWholPro     Best32. ;
FORMAT  ForeclosureProceed    Best32. ;
FORMAT  NonIntUPB      Best32. ;
FORMAT  PrinForgiv      Best32. ;
FORMAT  OriginalListDate    MMDDYY10. ;
FORMAT  OriginalListPrice    Best32. ;
FORMAT  CurrentListDate    MMDDYY10. ;
FORMAT  CurrentListPrice    Best32. ;
FORMAT  FICO_Borr_Iss     3.   ;
FORMAT  FICO_CoBorr_Iss    3.   ;
FORMAT  FICO_Borr_Curr     3.   ;
FORMAT  FICO_CoBorr_Curr    3.   ;
FORMAT  MI_Type      $1.  ;
FORMAT  ServicingActivity_Flag   $1.  ;
FORMAT  CurrModLoss     Best32. ;
FORMAT  CumModLoss      Best32. ;
FORMAT  CurrCreditEventLoss   Best32. ;
FORMAT  CumCreditEventLoss    Best32. ;
FORMAT  Homeready      $1.  ;
FORMAT  ForPrinWriteOff    Best32. ;
FORMAT  Relocation      $1.  ;
FORMAT  ZeroChangeDate     $6.  ;
FORMAT  Holdback      $1.  ;
FORMAT  HoldbackDate     $6.  ;
FORMAT  DelqInterest     Best32. ;
FORMAT  PropInspWaiver     $1.  ;
FORMAT  HighBalLoan     $1.  ;
Input
PoolID        $
LoanID        $
AsOfDate       $
Channel       $
Seller        $
Servicer        $
MasterServicer      $
ORate
CRate
OrigUPB
IssUPB
CurrentUPB
Term
OrigDate       $
FPDate        $
Age
RMLM
RMM
Maturity       $
oLTV
oCLTV
Borrowers
DTI
FICO_Borr
FICO_CoBorr
FTBuyer       $
Purp        $
Prop        $
Units
Occ        $
State        $
MSAcode
Zip
MI_Cov
Prod        $
Prep_Prem       $
IO_Flag       $
IO_FPDate       $
IO_MthsToAmort
DlqStatusCode      $
PayHist        $
modflag        $
MICancel_Flag       $
zerocode        $
zerodate       $
UPBRemoval
RepurchaseDate      $
SchedPrin
TotalPrin
UnSchedPrin
LPIDate
ForeclosureDate
DispositionDate
ForeclosureCost
MainPres
RecoveryCost
MiscExp
TaxInsu
NetSalPro
MICredit
RepMakeWholPro
ForeclosureProceed
NonIntUPB
PrinForgiv
OriginalListDate
OriginalListPrice
CurrentListDate
CurrentListPrice
FICO_Borr_Iss
FICO_CoBorr_Iss
FICO_Borr_Curr
FICO_CoBorr_Curr
MI_Type       $
ServicingActivity_Flag    $
CurrModLoss
CumModLoss
CurrCreditEventLoss
CumCreditEventLoss
Homeready       $
ForPrinWriteOff
Relocation       $
ZeroChangeDate      $
Holdback       $
HoldbackDate      $
DelqInterest
PropInspWaiver      $
HighBalLoan      $
;

NOTE: The data set WORK.CIRTDATA has 1 observations and 0 variables.
NOTE: DATA statement used (Total process time):
      real time           0.21 seconds
      cpu time            0.20 seconds

Super User
Posts: 9,868

Re: SAS macro to read a text file

Posted in reply to rramcharan

Your macro has lots of %put statements that write text to the log, but it does not create any code besides

   DATA &OutputData.;

        run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 3

Re: SAS macro to read a text file

Posted in reply to KurtBremser

yes good point, this now works

 

%macro InputSetupData (CIRTASIS=,  InputFile=, OutputData=, Deal= , paramtab= );

%getinfile(&paramfile, &paramtab)  ;

%assignmacros(&paramtab);

 
   DATA &OutputData.;

  %put INFILE "&InputFile" DELIMITER = '|' MISSOVER SCANOVER DSD LRECL=32767 /*FIRSTOBS=2*/;
  INFILE "&InputFile" DELIMITER = '|' MISSOVER SCANOVER DSD LRECL=32767 /*FIRSTOBS=2*/;

    %do i=1 %to &nobs;

        INFORMAT  &&field&i &&informat&i;

    %end;

      
    %do i=1 %to &nobs;

        FORMAT  &&field&i &&format&i;

    %end;


    Input
    %do i=1 %to &nobs;
                 &&field&i  &&input&i

    %end;

        ;

        run;

%mend;


%InputSetupData(CIRTASIS=CIRT,  InputFile=&CIRT_Input, OutputData=CIRTData, Deal= , paramtab=Input_Fields_CS_CIRT_Setup_Data);

Super User
Posts: 9,868

Re: SAS macro to read a text file

Posted in reply to rramcharan

Instead of your nested macros, I'd have one data _null_ step that creates the actual data step with call execute from &paramtab.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,397

Re: SAS macro to read a text file

Posted in reply to rramcharan

Please use a code window - its the {i} when posting code to avoid smiley faces and to preserve formatting.  

First question I had was with the thread title - "SAS macro to read a text file" - why do you need to read a text file with macro?  Macro is not a replacement for Base SAS!  

Now I am assuming that the file specification is written down somewhere, possibly in an agreement?  So there is no need for macro at all, write a plain simple datastep read in.  This will make your coding life easier, and be easier to maintain.

If of course your trying to read in a file which you do not have the spec for you have no hope really. 

Ask a Question
Discussion stats
  • 4 replies
  • 72 views
  • 1 like
  • 3 in conversation