SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rakkoo
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

2 REPLIES 2
art297
Opal | Level 21

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

Rakkoo
Fluorite | Level 6

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

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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