DATA Step, Macro, Functions and more

Create new variables based on value of another variable

Reply
Frequent Contributor
Posts: 75

Create new variables based on value of another variable

Hi,

I have some data in which I want to conver the value of some variables into new variables.

Here's some data:
[pre] data tmp;
input AMT01 AMT02 AMT03 AMT04 CODE01 $ CODE02 $ CODE03 $ CODE04 $;
datalines;
100 200 500 400 AB CD EF GH
100 200 500 400 AB CD IJ KL
100 200 500 400 AB EF IJ KL
;
run;[/pre]

And here's how the table looks:
[pre] AMT01 AMT02 AMT03 AMT04 CODE01 CODE02 CODE03 CODE04
100 200 500 400 AB CD EF GH
100 200 500 400 AB CD IJ KL
100 200 500 400 AB EF IJ KL[/pre]

Here's how I want that data to look:
[pre] AB CD EF GH IJ KL
100 200 500 400 . .
100 200 . . 500 400
100 . 200 . 500 400[/pre]

Is there some simple method of creating the variable names without using macros? Hopefully with a function?


Thanks. Message was edited by: John T
Frequent Contributor
Posts: 81

Re: Create new variables based on value of another variable

data tmp1;
length code $8;
set tmp;
ord=_n_;
amt=amt01;
code=code01;
output;
amt=amt02;
code=code02;
output;
amt=amt03;
code=code03;
output;
amt=amt04;
code=code04;
output;
run;

proc transpose data=tmp1 out=tmp2(drop=ord _name_);
id code;
var amt;
by ord;
run;
Frequent Contributor
Posts: 75

Re: Create new variables based on value of another variable

Hi Nick R,

Thanks for your suggestion.

That works for me, now to integrate it into my job.
Ask a Question
Discussion stats
  • 2 replies
  • 213 views
  • 0 likes
  • 2 in conversation