Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am trying to run a macro that has

Status
Not open for further replies.

nando1856

MIS
Sep 25, 2003
5
0
0
US
I am trying to run a macro that has state names in it and there seem to be problems with Oregon because of it's abbreviation. I created a simpler version of the program for testing and I am pasting it below. Can anybody get this to work.



DATA MASTER; INPUT ID $ STATE $ SOURCE $ ;

DATALINES ;

A1 AL PHONE
A2 AL PHONE
A3 KY PHONE
A4 KY PHONE
A5 OR PHONE
A6 OR PHONE
A7 SD PHONE
A8 SD PHONE
A9 VA PHONE
AA VA PHONE
AB VA PHONE
AC FL PHONE
AD FL PHONE

RUN;

%MACRO STATE(ST=);


DATA MASTER1; SET MASTER;
IF STATE="&ST.";
RUN;

%IF &ST=OR OR &ST=VA %THEN %DO;
DATA WORK.Reg1_&ST.; SET WORK.MASTER; RUN; %END;
%IF &ST=FL OR &ST=SD OR &ST=KY %THEN %DO;
DATA WORK.Reg2_&ST.; SET WORK.MASTER; RUN; %END;

%MEND;


%STATE(ST=FL);
%STATE(ST=KY);
%STATE(ST=SD);
%STATE(ST=VA);
%STATE(ST=%BQUOTE(OR));
 
Your problem is with the lines likes this:

%IF &ST=OR OR &ST=VA %THEN %DO;

The state values OR and VA should be specified as literals making the code look like this

%IF &ST='OR' OR &ST='VA' %THEN %DO;

At present SAS is trying to resolve OR and VA as variables.

Hope this does the biz for you.
 
I tried that and it did not work. If you change OR to another state abbreviation the original logic it works. There has to be a way to mask OR so that the macro will resolve properly.
 
Here is an example of the same logic substituting MD for OR.
Try running both and compare the results to see what I am talking about.

*-----------------------------------------------;


DATA MASTER; INPUT ID $ STATE $ SOURCE $ ;

DATALINES ;

A1 AL PHONE
A2 AL PHONE
A3 KY PHONE
A4 KY PHONE
A5 MD PHONE
A6 MD PHONE
A7 SD PHONE
A8 SD PHONE
A9 MT PHONE
AA MT PHONE
AB MT PHONE
AC FL PHONE
AD FL PHONE

RUN;

%MACRO STATE(ST=);


DATA MASTER1; SET MASTER;
IF STATE="&ST.";
RUN;

%IF &ST.=MD OR &ST.=MT %THEN %DO;
DATA WORK.Reg1_&ST.; SET WORK.MASTER1; RUN; %END;
%IF &ST.=FL OR &ST.=SD OR &ST.=KY %THEN %DO;
DATA WORK.Reg3_&ST.; SET WORK.MASTER1; RUN; %END;

%MEND;


%STATE(ST=FL);
%STATE(ST=KY);
%STATE(ST=SD);
%STATE(ST=VA);
%STATE(ST=MD);
 
Change
%IF &ST=OR OR &ST=VA %THEN %DO;
to
%IF &ST=%STR(OR) OR &ST=VA %THEN %DO;
 

Well, You need to change a bit more.
From:
%IF &ST=OR OR &ST=VA %THEN %DO;
DATA WORK.Reg1_&ST.; SET WORK.MASTER; RUN; %END;

To:
%IF &ST=%str(OR) OR &ST=VA %THEN %DO;
DATA WORK.Reg1_%trim(&ST); SET WORK.MASTER; RUN; %END;
 
Tried that. Does not work. Run it yourself and you will see that it gives an error.
 
I have tested it before I posted. Could you post your log so I know what you are talking about.
 
You are right. It does work. I did not copy it correctly the first time. Thanks and Sorry about that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top