Thursday, March 13, 2008

Create and redefine variables

/*************************************/
/* create and redefine variables */
/* with assignment, function */
/*************************************/
data diabetes_female;
set diabetes;
if sex='F';
GlucoseChange=postglucose-fastglucose;
fastglucose=fastglucose+fastglucose*.10;
AvgGlucose=mean(postglucose,fastglucose);
run;

/*************************************/
/* create variables with LENGTH */
/*************************************/
data PatientAccounts;
set diabetes(keep=id sex age);
length Group $ 8;
if age>=55 then group='Seniors';
else group='Under 55';
run;

You can use the LENGTH statement to create a variable and set the length of the variable. Here the LENGTH statement assigns a length to accommodate the longest value of the variable Group. The longest value is Under 55, which has eight characters. Because Group is a character variable, you must follow the variable name with a dollar sign ($).

For character variables, you must allow for the longest possible value in the first statement that uses the variable, because you cannot change the length with a subsequent LENGTH statement within the same DATA step. The maximum length of any character variable in the SAS System is 32,767 bytes. For numeric variables, you can change the length of the variable by using a subsequent LENGTH statement.

/*************************************/
/* create variable with FORMAT */
/*************************************/
data sales;
format Sale_Price 6.2;
Sale_Price=49.99;run;

/*************************************/
/* create variables with ATTRIB */
/*************************************/
data sales;
attrib Sale_Price format=6.2
label="Sale Price";
Sale_Price=49.99;
run;

No comments: