BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Walternate
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

3 REPLIES 3
ballardw
Super User

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.

Astounding
PROC Star

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.

Walternate
Obsidian | Level 7

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!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 1203 views
  • 3 likes
  • 3 in conversation