I'm trying to create 2 data files with a macro using conditional logic (I know there are other methods to generate these 2 files).

%MACRO SelectCount;

%if GenderID = 2 %then %do;

Data female200;
set cd4;
if Count<200 and count ne . and Prescribed1=2 and Prescribed2=1;        

%else %if GenderID=1 %then %do;

Data male200;
set cd4;
if Count<200 and count ne . and and Prescribed1=2 and Prescribed2=1;        

%MEND SelectCount;


proc print data=female200 (obs=20); Title3 'Females'; run;

proc print data=male200 (obs=20); Title3 'Males'; run;

I'm getting an error that the female200 and male200 data do not exist.

asked 03 Aug '11, 11:28

KwonC's gravatar image

accept rate: 100%

edited 08 Sep '11, 13:11

jay.l.stevens's gravatar image

jay.l.stevens ♦

KwonC, is "GenderID" a macro variable or a dataset variable? If it is a macro variable, then you need to preface it with an ampersand in the %IF/%ELSE %IF statements, such as "IF &GenderID = 1 %THEN %DO;". If it is instead a dataset variable within the CD4 dataset then you could get rid of the macro code and rewrite the program like the following:

DATA female200 male200;
   IF (count < 200) AND
      (count NE .) AND
      (Prescribed1 = 2) AND
      (Prescribed2 = 1);
   IF (GenderID = 1) THEN OUTPUT male200;
   ELSE IF(GenderID = 2) THEN OUTPUT female200;

If GenderID is a macro variable, then you will still have an issue, even after adding the ampersand prefix. Since you have only called the %SelectCount one time, one of the two result datasets will not have been created, so you will still have an error in the log file when you try to print the other one. A simple fix would be to move the PRINT procedures into the %IF/%ELSE %IF sections of your macro definition.


answered 05 Aug '11, 18:36

RichW's gravatar image

accept rate: 50%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 03 Aug '11, 11:28

Seen: 1,620 times

Last updated: 08 Sep '11, 13:11

powered by OSQA