Saturday, January 15, 2011

Replace the values of set one by values from set two using PROC FORMAT

/********************************************************************
Suppose we have data set one and two as below:
one has variable a and two has variable b;
a=(1 2 3)
b=(x y z)
What we want to do is, if we have a new data set expressed in numerical values,
we want to change its numerical numbers 1, 2, 3 to character x, y, z .
For example, if we have
x=(1 2 3 5 7 9), we want to have y as
y=(x y z 5 7 9).
Or generally, replace the values of data set one by the values of data set two.
********************************************************************/

data one;
  input a;
  cards;
  1
  2
  3
  ;
run;

data two;
   input b $;
   cards;
   a
   b
   c
   ;
run;

data fmtds;
   merge one two;
   rename a=start b=label;
   end=a;
   fmtname='newfmt';
run;
proc print data=&syslast;
run;

proc format library=work cntlin=fmtds;
run;

data test;
   input x;
   y=put(x,newfmt.);
   cards;
   1
   2
   3
   4
   5
   6
   ;
run;

proc print data=&syslast;
run;


/*******************************************************
The result is shown as below:

                        Obs    x    y
                         1     1    a
                         2     2    b
                         3     3    c
                         4     4    4
                         5     5    5
                         6     6    6
********************************************************/

A useful paper to read:
10 things to know about proc format
10 thinks to know about proc format

No comments:

Post a Comment