DATA Step, Macro, Functions and more

Help transforming variables

Reply
New Contributor Don
New Contributor
Posts: 3

Help transforming variables

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.

PROC Star
Posts: 7,467

Help transforming variables

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;

PROC Star
Posts: 7,467

Re: Help transforming variables

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;

New Contributor Don
New Contributor
Posts: 3

Re: Help transforming variables

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!

Ask a Question
Discussion stats
  • 3 replies
  • 143 views
  • 6 likes
  • 2 in conversation