DATA Step, Macro, Functions and more

Creating new variables using a macro

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 138
Accepted Solution

Creating new variables using a macro

Hi,

Basically, I am trying to create 10 new variables from 10 old variables, where each of the ten new variables will be (old variable name)cat and have the same values as one another. I thought the easiest way would be to develop a macro like this:

%macro new(old);

data gym;

set in_r.gym;

if &old=1 then &old.cat='Once per month';

if &old=2 then &old.cat='2-3 times per month';

...etc.

%mend new;

%new(treadmill);

%new(steps);

%new(run);

etc.

The macro runs fine but then when I look at the final dataset, the new variables are no longer a part of it.

Any help is much appreciated!


Accepted Solutions
Solution
‎05-09-2013 02:50 PM
Super User
Posts: 5,497

Re: Creating new variables using a macro

Posted in reply to Walternate

Look at what the macro does.  Each time it runs, it recreates GYM based on the contents of IN_R.GYM.  So each time, there will be only one new variable in GYM.

Also, you have added the expense of reading in the entire original data set 10 times.

You would be better off with a shorter macro:

%macro new (old);

if &old=1 then &old.cat='Once per month';

else if &old=2 then &old.cat='2-3 times per month';

else ...;

%mend new;

Then you could run through the data just once, calling the macro for each variable:

data gym;

   set in_r.gym;

   %new (treadmill)

   %new (steps)

   ...

run;

Other points to consider ... will you need to assign a length to the new variables as part of the macro?  As it stands now, the length is set at 14 (the number of characters in "Once per month") which may not be long enough for some of the other values being assigned.  Also, as ballardw mentioned, perhaps you don't need to create the new variables at all?

Good luck.

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Creating new variables using a macro

Posted in reply to Walternate

Are you sure you need a new variable? A custom format may be easier;

proc format library=work;

value treadmill

1 = 'Once per month'

2 = '2-3 times per month'

3 = 'you get the idea'

/*one for each code value'

. = 'Missing/not recorded'

;

run;

proc frec data=gym;

tables treadmill;

format treadmill treadmill.;

run;

If the codes are similar across many topics then  a more general name like UseCode would be better as one format can be associated with many variables even if the variable doesn't use all of the codes as long as the code meanings are the same.

Solution
‎05-09-2013 02:50 PM
Super User
Posts: 5,497

Re: Creating new variables using a macro

Posted in reply to Walternate

Look at what the macro does.  Each time it runs, it recreates GYM based on the contents of IN_R.GYM.  So each time, there will be only one new variable in GYM.

Also, you have added the expense of reading in the entire original data set 10 times.

You would be better off with a shorter macro:

%macro new (old);

if &old=1 then &old.cat='Once per month';

else if &old=2 then &old.cat='2-3 times per month';

else ...;

%mend new;

Then you could run through the data just once, calling the macro for each variable:

data gym;

   set in_r.gym;

   %new (treadmill)

   %new (steps)

   ...

run;

Other points to consider ... will you need to assign a length to the new variables as part of the macro?  As it stands now, the length is set at 14 (the number of characters in "Once per month") which may not be long enough for some of the other values being assigned.  Also, as ballardw mentioned, perhaps you don't need to create the new variables at all?

Good luck.

Frequent Contributor
Posts: 138

Re: Creating new variables using a macro

Posted in reply to Astounding

That worked! Thanks.

I did in fact use a length statement; I just left it out because it wasn't relevant to my question. Thanks again!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 175 views
  • 3 likes
  • 3 in conversation