Help using Base SAS procedures

code explanation

Reply
Frequent Contributor
Posts: 122

code explanation

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;

Respected Advisor
Posts: 4,173

code explanation

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

Occasional Contributor
Posts: 18

code explanation

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.

PROC Star
Posts: 7,480

code explanation

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.


Respected Advisor
Posts: 4,173

code explanation

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

PROC Star
Posts: 7,480

Re: code explanation

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.

SAS Super FREQ
Posts: 8,868

Re: code explanation

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.

PROC Star
Posts: 7,480

code explanation

Posted in reply to Cynthia_sas

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;

Frequent Contributor
Posts: 122

code explanation

Thank you all for your valuable suggestions!

Ask a Question
Discussion stats
  • 8 replies
  • 255 views
  • 0 likes
  • 5 in conversation