Set new variable as value of a variable name

Solved
Frequent Contributor
Posts: 81

Set new variable as value of a variable name

Hi,

I'm stuck on something very simple..

I have a data step with an array that recodes a numeric into category variables, and i would like to add the text of the variable being coded as a prefix into the value of the category.

For example:

This is the code i am working with:

Data Table1 ; Set Data ;

Array num{*} var1 var2 var3 ;

Array Cat{*} cat1 cat2 cat3 ;

do i=1 to dim(num) ;

IF num{i} LE 50 then cat{i}='0-50';

IF num{i} GE 50 then cat{i}='0-100';

end ;

run ;

From this I get 3 category variables with 2 values '0-50' and '0-100'.

Cat1:  '0-50' and '0-100'

Cat2:  '0-50' and '0-100'

Cat3:  '0-50' and '0-100'

Essentially what I would like to do is add the prefix of num{i} into the value so it would look like:

Cat1:  'var1: 0-50' and 'var1: 0-100'

Cat2:  'var2: 0-50' and 'var2: 0-100'

Cat3:  'var3: 0-50' and 'var3: 0-100'

Accepted Solutions
Solution
‎10-16-2014 01:41 PM
Super User
Posts: 13,508

Re: Set new variable as value of a variable name

Try:

Cat{i} = catx(':',vname(num{i}),'0-50');

Cat{i} = catx(':',vname(num{i}),'0-100');

All Replies
Solution
‎10-16-2014 01:41 PM
Super User
Posts: 13,508

Re: Set new variable as value of a variable name

Try:

Cat{i} = catx(':',vname(num{i}),'0-50');

Cat{i} = catx(':',vname(num{i}),'0-100');

Frequent Contributor
Posts: 81

Re: Set new variable as value of a variable name

Perfect, thank you, I didn't know the vname function existed.

Super User
Posts: 23,683

Re: Set new variable as value of a variable name

Check your boundary conditions, you have GE 50 and LE 50, so where do you really want 50 to end up?

🔒 This topic is solved and locked.