DATA Step, Macro, Functions and more

Noob with Noob macro question

Posts: 23

Noob with Noob macro question

I have 2 macros I'm trying to run but neither are working. I've looked online but search engines return so much information it's overwhelming. Can anyone tell me 1) why the first set of code isn't working and 2) how to get the second to work?

The first macro searches through a data string to find a parameter value, then returns the datum within that string for that parameter:

%macro FindParseAfter(VarName, WhatItsLookingFor, NumberofCharacters);
format cmOutcome $300.
cmOutcome1 $300.
x 4.;
x=find(&VarName, &WhatItsLookingFor)+&NumberofCharacters;
%if find(&VarName,&WhatItsLookingFor) %then %do;
cmOutcome1 = substr(&VarName,x,300);
%if find(cmOutcome1, '&') %then %do;
cmOutcome = scan(cmOutcome1,1,'&');
%else %if find(cmOutcome1, '?') %then %do;
cmOutcome = scan(cmOutcome1,1,'?');
%else %if find(cmOutcome1, '#') %then %do;
cmOutcome = scan(cmOutcome1,1,'#');
%else %if find(cmOutcome1, "/") %then %do;
cmOutcome = scan(cmOutcome1,1,"/");
%else cmOutcome = cmOutcome1;


I have no idea why that won't run. It seems fine to me. It probably has something to do with quotes and the variable WhatItsLookingFor but I have no idea. Also, all parameters in the string are separated by a ?, &, / or #.

This one I get why it's not running. There are procs and data steps throughout the macro. This makes for obvious problems when it comes time to invoke the macro. The point of this macro is to match two dataset. One is a complete dataset. The other is a key. For instance. You may have data with zip codes in it. That's nifty and all but it's a little overwhelming because there's TONS of zip codes. How are you going to report on that? With a key that maps zip codes to cities, this code would create a new variable for cities and match everything up in a new dataset. Then reporting might done on cities instead of zip codes.

But SAS can't read:

data NewDataStep;

matchfromkey(Cities&Zips, WorkingDatabase, NewDataStep);


That wouldn't do anything but anger SAS. And nobody likes an angry SAS.

So I have no idea how to get this to work. Here's the code:

%macro matchfromkey(data_key, data, byVariable,outdata);
proc sort data=&data_key; by &byVariable;

proc sort data=&data; by &byVariable;

data &outdata;
merge &data_key(in=u) &data(in=a);
by &byVariable;

if u=1 and a=1 then do; ogilvy=1; output; end;
if u=0 and a=1 then do; ogilvy=2; output; end;


Thanks in advance! Message was edited by: Doxastic
Posts: 8,742

Re: Noob with Noob macro question

Possibly a good introduction to the SAS Macro facility would help clarify things. This is a very straightforward overview of just about everything that you can do with SAS Macro programs -- how to define them, how to invoke them, how to pass parameters, how to use conditional logic. I think it would be very useful to you.

Super User
Posts: 9,671

Re: Noob with Noob macro question

I saw something maybe to change.

if u=1 and a=1 then do; ogilvy=1; output; end;
if u=0 and a=1 then do; ogilvy=2; output; end;


if u and a then do; ogilvy=1; output; end;
if not u then do; ogilvy=2; output; end;


Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation