Build variable using other variables

Reply
Occasional Contributor Rik
Occasional Contributor
Posts: 12

Build variable using other variables

I want to create a variable where the name should contain the value of another variable.

Example :

A=1;
Within 1 data step, I want to create variable "SCALE1" where 1 represents the value of variable A.

Any suggestion is welcome
N/A
Posts: 0

Re: Build variable using other variables

Rik,

Try this:

data new;
A=1;
IF A = 1 THEN CALL SYMPUT('var1','scale'||LEFT(PUT(A, 3.)));
&var1 =45;
run;

proc print data=new;
run;
Occasional Contributor Rik
Occasional Contributor
Posts: 12

Re: Build variable using other variables

a Message was edited by: Rik
Occasional Contributor Rik
Occasional Contributor
Posts: 12

Re: Build variable using other variables

Hi Nilan,

This seems to be working fine in the example you posted but I also want to make it work in following example :
data test;
a=1;output;
a=2;output;
a=3;output;
run;
data new;
set test;
......
run;

Unfortunally your solution does not cover this...
But thanks for the effort.
Frequent Contributor
Posts: 95

Re: Build variable using other variables

I have two thoughts on this topic.

1) In the first solution, I'm surprised that call symput worked. I thought that variable assignment could not be used in the data set in which it was assigned. When I try the code myself, I get an error.

2) It sounds like you want to take a list of data and transpose it into a set of columns with the column name defined, in part, by a variable. To do this, I would suggest looking at the prefix option of PROC TRANSPOSE.

data test;
a=1;b=45; output;
a=2;b=27; output;
a=3;b=99; output;
run;

proc transpose data=test out=new prefix=Scale;
id a;
var b;
run;

proc print data=new; run;

Hope this helps.
Ask a Question
Discussion stats
  • 4 replies
  • 460 views
  • 0 likes
  • 3 in conversation