BookmarkSubscribeRSS Feed
rebelde52
Fluorite | Level 6

Hello, 

 

Is there a method where I can use/call a macro instead of using the same long lines of "IF-THEN" code for data cleaning? 

 

For example I have 4 different datasets and I use the same "IF-THEN" code to clean data from that data set. (they must be separate and not combined). Therefore, I think a macro could be used here instead of writing/copying the same long lines of code. Is this a possibility? 

 

My logic is :

 

Data Want;
set Have;

%macro

run;

 

Data Want1;
Set Have;
If then if then if then;
if then if then if then;
run

Data Want2;
set Have;
If then if then if then;
if then if then if then;
run; 

5 REPLIES 5
Reeza
Super User
Most definitely. If the combinations for recoding is simple, you can also consider a function or format instead of a macro.
ballardw
Super User

Are the values character or numeric?

Is the result numeric or character? Is the result based on a single variable?

Are you assigning a value to a new variable or back to the original variable or doing something else?

 

It might help to provide at least one concrete case with actual variables, values and code. There are often multiple ways to do something and the cleanest, easiest or most robust approaches may not be obvious.

 

For example if you have one variable and need to do one of multiple actions based on a single value then a SELECT/WHEN block may be easier code. If that doesn't look familiar that is why I suggest providing an actual example.

 

Astounding
PROC Star
Often, the best tool for applying the same logic is an array, not a macro. But as others have mentioned, you need to provide logic for a few variables for anyone to evaluate from a distance.
Patrick
Opal | Level 21

One of multiple options is to store your code in a file and then %include it in your data steps. What's the "right" approach will depend on your actual repeated logic and if it's fully static or needs some dynamic/parameterized component.

filename snippet temp;
data _null_;
  file snippet;
  input;
  put _infile_;
  datalines4;
if name='Alfred' then found=1;
else found=0;
;;;;

data demo;
  set sashelp.class;
  %include snippet/source2;
run;

 

Quentin
Super User

Yes, this is likely a good opportunity to try using the macro language.

 

The main purpose of the macro language is to generate SAS code.  So instead of you having to type SAS code yourself, the macro language can generate SAS code for you.

 

When I first started writing macros, one of the main triggers for me to consider writing a macro was when I found myself copying a block of code and pasting it multiple times (into one program, or multiple programs).  Placing that code inside a macro will allow you to store the code in one place (the macro definition) and then call the macro anywhere you want to generate that block of code.

 

As others have said, if you share an example of the data you have, and the IF statements you want to generate, people will be happy to help you develop the macro (and will also be happy to propose non-macro code generation approaches).

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 5 replies
  • 1268 views
  • 6 likes
  • 6 in conversation