BookmarkSubscribeRSS Feed
Doxastic
Calcite | Level 5
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,'&');
%end;
%else %if find(cmOutcome1, '?') %then %do;
cmOutcome = scan(cmOutcome1,1,'?');
%end;
%else %if find(cmOutcome1, '#') %then %do;
cmOutcome = scan(cmOutcome1,1,'#');
%end;
%else %if find(cmOutcome1, "/") %then %do;
cmOutcome = scan(cmOutcome1,1,"/");
%end;
%else cmOutcome = cmOutcome1;
%end;

%mend;

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);

run;

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;

run;
%mend;


Thanks in advance! Message was edited by: Doxastic
2 REPLIES 2
Cynthia_sas
SAS Super FREQ
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.
http://www2.sas.com/proceedings/sugi28/056-28.pdf

cynthia
Ksharp
Super User
I saw something maybe to change.

[pre]
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;

[/pre]


Ksharp

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 763 views
  • 0 likes
  • 3 in conversation