1
votes

So I've done some searching around online but haven't managed to find anything that can solve this problem. Essentially, I have been given a dataset that I've then split into individual dataset's based on name.

However, if the person is a female, the age needs to be omitted from the dataset. Example output:

Males

Name Age Weight Height

Females

Name Weight Height

I have tried the following IF statement, but it just seems to drop the age variable from both the male and female tables:

if sex="F" then do;
    drop age;
end;

I'm fairly new to SAS so any help would be greatly appreciated!

2

2 Answers

5
votes

When you run a data step in SAS, some statements are processed during compilation, and others subsequently during execution. In this case, the drop statement is processed before your if-then logic, so you can't use it to conditionally drop a column.

Alternatively, you could output a missing value for age for each affected row, e.g.

if sex = 'F' then call missing(age);

Or you could use a drop clause on one output dataset but not the other:

data boys girls(drop=age);
  set sashelp.class;
  if sex = 'F' then output girls;
  else if sex = 'M' then output boys;
run;
0
votes

The DROP statement cannot be run conditionally. You need to conditionally generate the DROP statement (or DROP= dataset option).

To use a trivial example dataset let's start with SASHELP.CLASS and split it into individual datasets. Note that this dataset only has one observation per NAME, but I will add BY group processing to the code generation step so you can see how you could use it in the case where there are multiple observations per name.

First let's generate code for single DATA statement that creates multiple output datasets. Based on the value of the SEX variable it will conditionally add a DROP= dataset option.

filename code temp;
data _null_;
  set sashelp.class end=eof ;
  by name ;
  file code ;
  if _n_=1 then put 'data' ;
  if first.name then do;
    put '  ' name @ ;
    if sex='F' then put '(drop=age)' @ ;
    put ;
  end;
  if eof then put ';' ;
run;

Now let's append the code for the rest of the data step that will read the source dataset and output the records to the appropriate dataset.

data _null_;
  set sashelp.class end=eof ;
  by name ;
  file code mod ;
  if _n_=1 then put '  set sashelp.class; ' ;
  if first.name then put '  if name =' name $quote. 'then output ' name ';' ;
  if eof then put 'run;' ;
run;

Finally run the generated code.

%include code / source2 ;