BookmarkSubscribeRSS Feed
renjithr
Quartz | Level 8

Hi,

I inherited a code and it i dont understand the meaning of the following piece ageg=1+1*(sex='F'); ,is it similar to a conditional check?

data tt;

set t;

ageg=1+1*(sex='F');

run;

8 REPLIES 8
Patrick
Opal | Level 21

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');

dav_amol
Calcite | Level 5

Hi,

   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.

art297
Opal | Level 21

It is an excessive way of coding:

ageg+(sex='F')

simply a count of the number of records that have a code of 'F' for the sex variable.


Patrick
Opal | Level 21

May be the OT's code is more about recoding (as ageg seems not to be retained)

art297
Opal | Level 21

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.

Cynthia_sas
SAS Super FREQ

Hi:

  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.

cynthia

3200  data tt;

3201    set sashelp.class;

3202    where age le 13;

3203    x = (sex='F');

3204    ageg=1+1*(sex='F');;

3205    put _n_= name= sex= x= ageg=;

3206  run;

          

_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.

      WHERE age<=13;

NOTE: The data set WORK.TT has 10 observations and 7 variables.

art297
Opal | Level 21

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:

data tt;

  set sashelp.class;

  if age eq 13 then do;

    call missing(sex);

  end;

  ageg=1+1*(sex='F');;

run;

renjithr
Quartz | Level 8

Thank you all for your valuable suggestions!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1923 views
  • 0 likes
  • 5 in conversation