SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

MACROS

Reply
Occasional Contributor
Posts: 17

MACROS

I would greatly appreciate it if someone could help me solve my current dilemma. I woulld like to create a macro that can be used in several datasteps. Below is what I have done so far. I keep getting error messages that the new variables (newrace2 and are livingage_new&year.) unitialized.

 

%macro try;
%if newrace= '4' %then %let newrace2='1. Black';
%if newrace= '1' %then %let newrace2='2. Hispanic';
%if newrace= '6' %then %let newrace2='3. White';
%if newrace= '3' %then %let newrace2='4. API &Other';
%if newrace= '5' %then %let newrace2='4. API &Other';
%if newrace= '7' %then %let newrace2='4. API &Other';
%if newrace= '2' %then %let newrace2='4. API &Other';
%if newrace= '8' %then %let newrace2='4. API &Other';
%if newrace= '9' %then %let newrace2='4. API &Other';
%if newrace= ' ' %then %let newrace2='4. API &Other';

%if 0 le livingage&year. le 24 %then %let livingage_new&year. ='0-24';
%if 25 le livingage&year. le 34 %then %let livingage_new&year. ='25-34';
%if 35 le livingage&year. le 44 %then %let livingage_new&year. ='35-44';
%if 45 le livingage&year. le 54 %then %let livingage_new&year. ='45-54';
%if 55 le livingage&year. %then %let livingage_new&year. ='55+';
run;
%mend try;

 

data estab0_demo2;
length newrace2 $13.;
length livingage_new&year $13.;
set estab1_demo;
%try;
run;

PROC Star
Posts: 1,090

Re: MACROS

Your statements should probably be:

 

if newrace= '4' then newrace2='1. Black';

 

etc. %if, %then, and %let statements control the functioning of the macro, not the underlying SAS code.

 

Also, if you use some "else" clauses, things will be a little clearer. There is also a "select" statement that you could consider.

 

Tom

 

Occasional Contributor
Posts: 17

Re: MACROS

THanks
Occasional Contributor
Posts: 17

Re: MACROS

Thank you for your help. Now that I have that done is there a macro in which i ould be able to minimize repeating the same step but with different datasets. As shown below after the macro is created the same macor is applied to 3 different datasets. I would like to further minimize repetition. 

 

%macro try;

if newrace= '4' then newrace2 = '1. Black';
if newrace= '1' then newrace2= '2. Hispanic';
if newrace= '6' then newrace2= '3. White';
if newrace in ('3', '5', '7', '2', '8', '9', ' ') then newrace2= '4. API &Other';

if 0 le livingage&year. le 24 then livingage_new&year.='0-24';
if 25 le livingage&year. le 34 then livingage_new&year.='25-34';
if 35 le livingage&year. le 44 then livingage_new&year.='35-44';
if 45 le livingage&year. le 54 then livingage_new&year.='45-54';
if 55 le livingage&year. then livingage_new&year.='55+';
%mend try;


data estab0_demo2;
length newrace2 $13.;
length livingage_new&year $13.;
set estab1_demo;
%try;
run;


data estab1_demo2;
length newrace2 $13.;
length livingage_new&year $13.;
set estab1_demo;
%try;
run;

 

data nr_demo2;
length newrace2 $13.;
length livingage_new&year $13.;
set nr_demo;
%try;
run;

Super User
Posts: 5,081

Re: MACROS

This code is actually the closest you have come to getting something working.  I would stick with the approach, but there is a key question to answer.

 

How do you expect &YEAR to be assigned a value?

PROC Star
Posts: 1,090

Re: MACROS

To shorten it even more, take a look in the help documentation about how to pass parameters into macros. If you pass the "estab0_demo" type information as a parameter, you could make the whole thing into a macro. Some similar examples are in:

 

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#macro-stmt.htm

 

Also, the advice about using formats is excellent. You should investigate those as well.

 

Tom

Super User
Super User
Posts: 7,401

Re: MACROS

Well, first off you don't seem to understand macro language.  Think of it this way, if you copy the the code in the macro and replace the call with it:

data estab0_demo2;
length newrace2 $13.;
length livingage_new&year $13.;
set estab1_demo;

%if newrace= '4' %then %let newrace2='1. Black';
%if newrace= '1' %then %let newrace2='2. Hispanic';
%if newrace= '6' %then %let newrace2='3. White';
%if newrace= '3' %then %let newrace2='4. API &Other';
%if newrace= '5' %then %let newrace2='4. API &Other';
%if newrace= '7' %then %let newrace2='4. API &Other';
%if newrace= '2' %then %let newrace2='4. API &Other';
%if newrace= '8' %then %let newrace2='4. API &Other';
%if newrace= '9' %then %let newrace2='4. API &Other';
%if newrace= ' ' %then %let newrace2='4. API &Other';

%if 0 le livingage&year. le 24 %then %let livingage_new&year. ='0-24';
%if 25 le livingage&year. le 34 %then %let livingage_new&year. ='25-34';
%if 35 le livingage&year. le 44 %then %let livingage_new&year. ='35-44';
%if 45 le livingage&year. le 54 %then %let livingage_new&year. ='45-54';
%if 55 le livingage&year. %then %let livingage_new&year. ='55+';
run;
run;

 

Does the above look like valid Base SAS code?  No.  

The second point I would ask is why?  What you are doing is coding what is essentially formats.  e.g.

proc format;
  value $newrace
    '4' = '1. Black'
    ...;
run;

data want;  
  set have;
  format race $newrace.;
run;

Please see the help docs:

http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a000063536.htm


Alternatively, if you have to do it in code, then create a dataset with your code values and decodes values:

CODE    DECODE

4            1. Black

...

Then merge that data onto your other data, far easier to maintain.

 

 

Occasional Contributor
Posts: 17

Re: MACROS

Thank you
Super User
Posts: 17,801

Re: MACROS

I'll second @RW9 recommendation of using a format instead. I'll also suggest using a double format for the race variable so it sorts without the extra 1, 2 in the labels. If you need help to code that let me know. 

 

A a good reference for formats is the paper titled Proc Format: Not Just Another Pretty Face

Ask a Question
Discussion stats
  • 8 replies
  • 458 views
  • 0 likes
  • 5 in conversation