02-21-2012 08:54 PM
The expression sex='F' will return 1 if true and 0 if false. If sex='F' then ageg will get the value assigned: 1+1*(1), else it will be: 1+1*(0).
I believe the following expression would do the same;
ageg= 1 +(sex='F');
02-21-2012 08:55 PM
You are right. The statement sex='F' is used as logical expression which compares the value of variable sex with 'F' .
1. If it matches it returns 1 so the sum result would be 2
2. If it doesn't matches then the logical expression returns 0 so final result would be 0.
02-21-2012 08:59 PM
Actually, the more I look at it, it is simply wrong unless one wants to assign the value 2 to the ageg varoab;e fpr every record that has a value of 'F' for sex.
02-21-2012 09:00 PM
With just a slight modification to your existing code, you can see what happens with the evaluation of (sex='F').
I changed your program to use a few records from SASHELP.CLASS and the LOG output below shows you how X is set to 0 or 1 based on the value of SEX. Therefore, AGEG is set to either 2 or 1 based on the value of the SEX variable.
3200 data tt;
3201 set sashelp.class;
3202 where age le 13;
3203 x = (sex='F');
3205 put _n_= name= sex= x= ageg=;
_N_=1 Name=Alice Sex=F x=1 ageg=2
_N_=2 Name=Barbara Sex=F x=1 ageg=2
_N_=3 Name=James Sex=M x=0 ageg=1
_N_=4 Name=Jane Sex=F x=1 ageg=2
_N_=5 Name=Jeffrey Sex=M x=0 ageg=1
_N_=6 Name=John Sex=M x=0 ageg=1
_N_=7 Name=Joyce Sex=F x=1 ageg=2
_N_=8 Name=Louise Sex=F x=1 ageg=2
_N_=9 Name=Robert Sex=M x=0 ageg=1
_N_=10 Name=Thomas Sex=M x=0 ageg=1
NOTE: There were 10 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.TT has 10 observations and 7 variables.
02-21-2012 09:28 PM
Cynthia is correct, of course, but if that was the intent, it will also score a 1 for any missing value of sex. e.g., try the following:
if age eq 13 then do;