DATA Step, Macro, Functions and more

Set new variable as value of a variable name

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 81
Accepted Solution

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'

Thank you for your help.


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

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');

View solution in original post


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

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: 19,789

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 191 views
  • 0 likes
  • 3 in conversation