vokey588
vokey588

Reputation: 203

Create variable in SAS that is calculated from other variables

I have a big data set called BRFSS with data on AGE, SEX, whether the respondent has had a mammogram (HADMAM), and how long it has been since their mammogram (HOWLONG). I would like to create a new calculated variable called _MAM502Y that is defined by this SAS code:

IF SEX=2 AND AGE GE 50 THEN DO;
 IF HADMAM=1 THEN DO;
       IF HOWLONG IN (1,2) THEN _MAM502Y=1;
  ELSE IF HOWLONG IN (3,4,5) THEN _MAM502Y=2;
  ELSE IF HOWLONG IN (7,9) THEN _MAM502Y=9;
  END;
 ELSE IF HADMAM=2 THEN _MAM502Y=2;
 ELSE IF HADMAM IN (7,9,.) THEN _MAM502Y=9;
 END;
ELSE IF SEX=2 AND AGE IN (.,7,9) THEN _MAM502Y=9;
ELSE  _MAM502Y=.;

I have a lot of experience using R for data manipulation but I need to create this variable in SAS. Could someone tell me how I use this code in SAS to create the new variable? Thank you!

Upvotes: 0

Views: 265

Answers (1)

vasja
vasja

Reputation: 4792

Say you have a table TABLE1 in SAS library LIB1.

To make the new variable a permanent part of the original table do this (rewrite the source table):

data LIB1.TABLE1;
  set LIB1.TABLE1;
  ... your code..
run;

To create new copy of source table + new field. just change data LIB1.TABLE1; above to e.g. data LIB1.TABLE2;

To create something like original data + new calculated field (not stored), create a SAS datastep view like this (this just defines the calculation (program), no data is written, so it's done in no time (good for playing):

data LIB1.VIEW1 / view = LIB1.VIEW1;
  set LIB1.TABLE1;
  ... your code..
run;

Upvotes: 1

Related Questions