DATA Step, Macro, Functions and more

Using a MACRO variable in the INFILE statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Using a MACRO variable in the INFILE statement

Hello. Would you please help me? I'm a beginner in the world of macro.

I have downloaded about 300 raw data files, monthly data between 1989 and 2013. I need only about 10 variables in each file. I have the list of the physical names of all the raw data files. Then, I have to create a new data file for each month. For example,

RAW DATA                                  NEW DATA

c:\cps\cpsb91\cpsb9111      -->      c:\cps\replicate\d199111.sas7bdat

c:\cps\cpsb91\cpsb9112      -->      c:\cps\replicate\d199112.sas7bdat

c:\cps\cpsb92\cpsb9201      -->      c:\cps\replicate\d199201.sas7bdat

c:\cps\cpsb92\cpsb9202      -->      c:\cps\replicate\d199202.sas7bdat

c:\cps\cpsb92\cpsb9203      -->      c:\cps\replicate\d199203.sas7bdat

. . . and so forth

So, I thought I had to use MACRO. The problem that I cannot solve is that the program below works beautifully only if the raw data file has no extension, such as ".dat"

______________________________________________________________________

libname replicat 'c:\cps\replicate';

%macro CPSMACRO/parmbuff;

%let i=1;

%let m=6;

%let y=1995;

%let prfx=d;

%let raw=%scan(&syspbuff,&i);

%do %while(&raw ne);

%let yyyymm=%eval(&y*100+&m);

%let fname=&prfx.&yyyymm;

data replicat.&fname.;

infile &raw. lrecl=2300 missover;                        <-- In this INFILE statement, I include the macro variable.

input

@1 hh 15.

@122 age 2.

@436 ind $3.

@439 occu $3.

;

run;

%if &m=12 %then %do;

  %let i=%eval(&i+1);

  %let m=1;

  %let y=%eval(&y+1);

  %let raw=%scan(&syspbuff,&i);

%end;

%else %do;

  %let i=%eval(&i+1);

  %let m=%eval(&m+1);

  %let raw=%scan(&syspbuff,&i);

%end;

%end;

%mend CPSMACRO;

%CPSMACRO(                                 <-- I run the macro and input the physical name of three raw data files, for example.

'C:\cps\cpsb95\cpsb9506'

'C:\cps\cpsb95\cpsb9507'

'C:\cps\cpsb95\cpsaug95.dat');

______________________________________________________________________

This macro works only for the first two data files, and yields an error message (ERROR: Literal contains unmatched quote) for the third one.

I've tried both single-quote and double-quote, but neither works. Would you please help me? Thank you so very much.

Rakkoo


Accepted Solutions
Solution
‎10-15-2013 06:32 PM
PROC Star
Posts: 7,356

Re: Using a MACRO variable in the INFILE statement

Without testing your code, I will guess that your problem is simply that periods are being treated as delimiter when you call the scan function. You can control what is, and isn't used as a delimiter, by specifying the modifier option.  Take a look at:

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

View solution in original post


All Replies
Solution
‎10-15-2013 06:32 PM
PROC Star
Posts: 7,356

Re: Using a MACRO variable in the INFILE statement

Without testing your code, I will guess that your problem is simply that periods are being treated as delimiter when you call the scan function. You can control what is, and isn't used as a delimiter, by specifying the modifier option.  Take a look at:

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Occasional Contributor
Posts: 9

Re: Using a MACRO variable in the INFILE statement

Excellent! It was the delimiter! Now the macro works. Thanks a lot!  Smiley Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 956 views
  • 0 likes
  • 2 in conversation