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
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:
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:
Excellent! It was the delimiter! Now the macro works. Thanks a lot!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.