DATA Step, Macro, Functions and more

recoding variable around on median of variable

Reply
Contributor
Posts: 58

recoding variable around on median of variable

So I have a variable called cumlative dosage. I want to code a patient to see if they are above or the below the median dosage.

So when I run:

proc means data=temp n min max median;

     var avcumdos_6wper;

     run;

This is the following output

                                           N         Minimum         Maximum          Median

                                         ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

                                         184       1.1695906      10.4000000      4.0354701

                                         ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ

So I want to calculate a flag variable around that mean.  where flag=. if avcumdos_6wper=.,  flag=1 if avcumdos_6wper <= median or flag=2 if avcumdos_6wper> median.

So I figure I can do this via sql, but i can't seem to get it to work. It just copies the variable. This is my code thus far:

proc sql;

create table temp1 as

select patientid, visit, avcumdos_6wper, median(avcumdos_6wper) as mv,

  case when avcumdos_6wper = .  then .

  when avcumdos_6wper <= calculated mv then 1

       when avcumdos_6wper > calculated mv then 2

       else 999 end as flag

from temp;

Can someone tell me what I am doing wrong?

Super User
Posts: 19,787

Re: recoding variable around on median of variable

Are you on SAS 9.4? Before SAS 9.4 proc SQL cannot calculate the median.

Super User
Posts: 11,343

Re: recoding variable around on median of variable

If median works it is only working on one record at a time with your syntax. My current SAS does not have the median function available in SQL but you may be looking for something like:

proc sql;

create table temp1 as

select patientid, visit, avcumdos_6wper, b.mv,

  case when avcumdos_6wper = .  then .

  when avcumdos_6wper <= b.mv then 1

       when avcumdos_6wper > b.mv then 2

       else 999 end as flag

from temp join (select median(avcumdos_6wper) as mv from temp) as b;

quit;


Ask a Question
Discussion stats
  • 2 replies
  • 210 views
  • 0 likes
  • 3 in conversation