BookmarkSubscribeRSS Feed
Don
Calcite | Level 5 Don
Calcite | Level 5

I thought this would be simple, but nothing I have tried works......I have hundreds of variables I need to rescale (1to 5 instead of 5 to 1).  Calculation is easy: varnew=6 - varold.  But I don't want to do that several hundred times.  I was trying to put the list of variable names in a macro variable and then break that apart using a scan function.  Then I was adding an 'r' as a suffix and calculating the new value for that variable based on the old.  I need to keep both variables.  Any ideas?  I would post what I tried but network went down so I can't retrieve right now.  Thanks in advance for any thoughts.

3 REPLIES 3
art297
Opal | Level 21

Don,

Would something like the following do what you need?

data have;

  input x y z;

  cards;

1 2 3

5 2 1

4 3 4

;

data want (drop=i);

  set have;

  array recode(*) _numeric_;

  do i=1 to dim(recode);

    recode(i)=6-recode(i);

  end;

run;

art297
Opal | Level 21

Don,

I didn't notice the part that you wanted to keep the original variables.  There may be an even easier way but, as long as you can enter the starting and ending variables, then you may be able to use something like:

data have;

  input x y z;

  cards;

1 2 3

5 2 1

4 3 4

;

proc sql noprint;

  select strip(name)||"=r_"||strip(name),strip(name)

    into :renames separated by " ",

         :keeps separated by " "

      from dictionary.columns

        where libname="WORK"

          and memname="HAVE"

          and type="num"

  ;

quit;

data want (drop=i);

  set have;

  set have (keep=&keeps. rename=(&renames.));

  array recode(*) r_x--r_z;

  do i=1 to dim(recode);

    recode(i)=6-recode(i);

  end;

run;

Don
Calcite | Level 5 Don
Calcite | Level 5

Art,

Thanks for the ideas!  There were a couple of peculiarities in my data,  but I was able to take your idea and make it work perfectly!  For some reason I was stuck in a specific thought pattern.  Thanks again!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1047 views
  • 6 likes
  • 2 in conversation