BookmarkSubscribeRSS Feed
beverlyobeng
Calcite | Level 5

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;

8 REPLIES 8
TomKari
Onyx | Level 15

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

 

beverlyobeng
Calcite | Level 5
THanks
beverlyobeng
Calcite | Level 5

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;

Astounding
PROC Star

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?

TomKari
Onyx | Level 15

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

 

beverlyobeng
Calcite | Level 5
Thank you
Reeza
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1328 views
  • 0 likes
  • 5 in conversation