## Use value of variable as variable name

Occasional Contributor
Posts: 14

# Use value of variable as variable name

MED01      HTN  DM   MAIN01

HTN           0       0

DM            1       1

HTN            0      1

HTN            0      0

DM             0      1

I have a large database with information on medications taken by a few thousand people. I need to determine if a medication is a maintenance medication.

As an example above is a small snippet just looking at one medication for 5 people and a couple of disease conditions, HTN and DM

I need to compute the value of MAIN01...1 if a medication is a maintenance med and 0 if not

For example for the first observation, I look at the value of MED01 and I see that a HTN drug was used so  I need to check if person has HTN .  If HTN=1 then MAIN01=1 else MAIN01=0.

For the second observation, I look at the value of MED01 and see that I need to check if person has DM.  If DM=1 thenn MAIN01=1 else MAIN01=0

So the resulting data set would be

MED01      HTN  DM   MAIN01

HTN           0       0         0

DM            1       1         1

HTN            0      1         0

HTN            1      0         1

DM             0      1         1

Any suggestions as to how I could code this?

thanks!

Barbara

Frequent Contributor
Posts: 129

## Re: Use value of variable as variable name

Main01 = ( med01 = 'HTN')*htn  or (med01 = 'DM')*dm ;

If logical expressions are not you cup of tea, you could also use if-then logic.

Occasional Contributor
Posts: 14

Thanks!

PROC Star
Posts: 8,164

## Re: Use value of variable as variable name

Assuming there is a discrepancy in the data you showed in your have and want examples (namely for the 4th record), then the following would be easy to implement for any number of drugs:

data have;

input MED01  \$    HTN  DM;

cards;

HTN           0      0

DM            1      1

HTN           0      1

HTN           1      0

DM            0      1

;

data want;

set have;

array drugs htn--dm;

do over drugs;

if med01 eq vname(drugs) and drugs eq 1 then MAIN01=1;

end;

if missing(MAIN01) then MAIN01=0;

run;

Occasional Contributor
Posts: 14

## Re: Use value of variable as variable name

Thanks!

Discussion stats
• 4 replies
• 326 views
• 6 likes
• 3 in conversation