SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
GingerJJ
Obsidian | Level 7
Hi, I have the following MACRO %_LINKEX (DATA1=aa, DATA2=bb, LMIN=2); %_BY (DOB,FN1,LN1); %_VAR (P_FN1,P_LN1); %_RUN; The variable I have are: FN1, FN2, LN1, LN2 P_FN1, P_FN2, P_LN1, P_LN2 If I have to do it individually, I basically have to run 16 combinations. eg. combination 1: FN1, LN1 with P_FN1, P_LN1 combination2: FN1, LN2 with P_FN1, P_LN1. I have another macro which was used in another program, like this: %MACRO COMPARE(FN, NFN, LN, NLN); (%DO C = 1 %TO &NFN; %DO A= 1 %TO &FLN; (&FN.&F=&LN.&L AND &FN.&F IS NOT NULL) %IF &F = &NFN AND &L = &NLN %THEN %DO; %END; %ELSE %DO; OR %END; %END; %END;) %MEND EXACT; I was wondering how I can combine the two so that I don't need to run 16 times of each combination. Thanks!
15 REPLIES 15
Tom
Super User Tom
Super User

Can you retype your question so it is readable?  At least insert some line breaks.

For blocks of code use the Insert Code or Insert SAS Code icon on the edit window menu bar to get a pop-up window where you can insert or edit the blocks of code so that the formatting is preserved.

GingerJJ
Obsidian | Level 7

Sorry! Not sure what happened that the format completely changed in the post. Try again here.

 I have the following

MACRO %_LINKEX (DATA1=aa, DATA2=bb, LMIN=2);

%_BY (DOB,FN1,LN1);

%_VAR (P_FN1,P_LN1);

%_RUN;

 

The variable I have are: FN1, FN2, LN1, LN2 P_FN1, P_FN2, P_LN1, P_LN2

If I have to do it individually, I basically have to run 16 combinations.

eg. combination 1: FN1, LN1 with P_FN1, P_LN1

combination2: FN1, LN2 with P_FN1, P_LN1.

 

I have another macro which was used in another program, like this:

%MACRO COMPARE(FN, NFN, LN, NLN);

(%DO C = 1 %TO &NFN; %DO A= 1 %TO &FLN;

(&FN.&F=&LN.&L AND &FN.&F IS NOT NULL)

%IF &F = &NFN AND &L = &NLN %THEN %DO;

%END; %ELSE %DO;OR %END; %END; %END;)

%MEND COMPARE;

 

I was wondering how I can combine the two so that I don't need to run 16 times of each combination. Thanks!

Kurt_Bremser
Super User

Show us the SAS code you want to create for a single instance, and define which parts need to be made dynamic so they can be used as macro parameters.

GingerJJ
Obsidian | Level 7

%_LINKEX (DATA1=aa, DATA2=bb, LMIN=2);
%_BY (DOB,FN1,LN1);
%_VAR (P_FN1,P_LN1);
%_RUN;

DATA link1;
set _LKD
      _TIE;
run;

 

I basically will have link1-link16 and join them together and dedup.

GingerJJ
Obsidian | Level 7

Another problem I just realized that each time the macro will generate two files: _LKD and _TIE.

If I insert another macro in it, I may just get the last combination instead of the all of them together....

Tom
Super User Tom
Super User

@GingerJJ wrote:

%_LINKEX (DATA1=aa, DATA2=bb, LMIN=2);
%_BY (DOB,FN1,LN1);
%_VAR (P_FN1,P_LN1);
%_RUN;

DATA link1;
set _LKD
      _TIE;
run;

 

I basically will have link1-link16 and join them together and dedup.


What you posted looks like calls to 4 different macros followed by a data step. You have not show any definitions for any of these four macros.

 

The macro language in SAS is used to generate SAS code.   

What is the SAS code you are trying to run?  How can the macro processor help you generate that code?

Tom
Super User Tom
Super User

What those this mean?

I basically will have link1-link16 and join them together and dedup.

Are you saying you want to combine 16 datasets and get the unique set of observations?

data all;
  set link1-link16;
run;
proc data data=all nodupkey ;
  by _all_;
run;

 

 

Reeza
Super User
I suspect OP is working with survey data over several periods of time and needs to link those? Possibly....
Kurt_Bremser
Super User

@GingerJJ wrote:

%_LINKEX (DATA1=aa, DATA2=bb, LMIN=2);
%_BY (DOB,FN1,LN1);
%_VAR (P_FN1,P_LN1);
%_RUN;

DATA link1;
set _LKD
      _TIE;
run;

 

I basically will have link1-link16 and join them together and dedup.


That's just macro calls, we need to see the code you want to create wirh those macros, and the code of the macros themselves, so we can point out what goes wrong.

If these are macros you downloaded from somewhere, see the documentation there, and call for the developer's help.

Tom
Super User Tom
Super User

Are you seriously trying to use the ancient MACRO statement instead of the normal %MACRO statement?
If so your syntax does not appear to be valid

667   MACRO
ERROR: Old-style macro name % must contain only letters, digits, and underscores.
667 !       %_LINKEX (DATA1=aa, DATA2=bb, LMIN=2);
668
669   %_BY (DOB,FN1,LN1);
       ---
       180

ERROR 180-322: Statement is not valid or it is used out of proper order.

WARNING: Apparent invocation of macro _VAR not resolved.
670
671   %_VAR (P_FN1,P_LN1);
      -
      180

ERROR 180-322: Statement is not valid or it is used out of proper order.

WARNING: Apparent invocation of macro _RUN not resolved.
672
673   %_RUN;
      -
      180

ERROR 180-322: Statement is not valid or it is used out of proper order.

GingerJJ
Obsidian | Level 7

LINKEX is package for the purpose of data linkage.

I don't know if I understand what you mean by ancient or normal. I'm pretty new to MACRO

Reeza
Super User
Forget macros.

What are you trying to do? Packages isn't a term that's used in SAS really.
Is LINKEX an internal macro to your company that you're trying to use? Did you find it somewhere? Can you share the code?

I genuinely have no idea what that code above could be doing right now.....and I've programmed in SAS for almost 2 decades.
Reeza
Super User

If you're new to macros here are some references. 

Generally, macros inside macros is a no. You can call macros within macros but you shouldn't define macros within macros. 

 

General guideline to making macros is first have working code, then figure out how to generalize it. Can you show how your code would be if there were no macros?

 

UCLA introductory tutorial on macro variables and macros

https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

Tutorial on converting a working program to a macro

This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

Tom
Super User Tom
Super User

@GingerJJ wrote:

LINKEX is package for the purpose of data linkage.

I don't know if I understand what you mean by ancient or normal. I'm pretty new to MACRO


What is LINKEX?  How does it work? We cannot help you with something we know nothing about.  

 

Here is the only LINKEX I could find and it has nothing to with programming, let alone SAS programming.

https://trackapkg.com/linkex-tracking-number

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 15 replies
  • 2415 views
  • 10 likes
  • 5 in conversation