LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (November 2008, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 17 Nov 2008 13:57:30 +0530
Reply-To:   Anindya Mozumdar <anindya.lugbang@GMAIL.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Anindya Mozumdar <anindya.lugbang@GMAIL.COM>
Subject:   Re: what's the difference between %If and if in data step
Comments:   To: "he.terry@gmail.com" <he.terry@gmail.com>
In-Reply-To:   <7c6bf3c1-3f6b-43f1-8324-77c6a7141031@b38g2000prf.googlegroups.com>
Content-Type:   text/plain; charset=ISO-8859-1

On Mon, Nov 17, 2008 at 1:30 PM, he.terry@gmail.com <he.terry@gmail.com> wrote: > When should I use %if? When should I use if?

%if should be generally be used when you are trying to control the generated data step (or for that matter, any code) which should be submitted to the SAS compiler. The IF statement should be used for conditional processing or subsetting in a data step. Look at the examples below -

Example of %if

%macro restrictdata(channelName=);

data out; set in; %if &channelName ^= %then %do; where channel = "&channelName"; %end; run;

%mend restrictdata;

%restrictdata(channelName=) would generate the data step

data out; set in; run;

%restrictdata(channelName=INTERNET) would generate the data step

data out; set in; where channel = "INTERNET"; run;

Example of conditional IF -

data grade; set marks;

/* The variable grade takes a value depending on the value of the variable science. A very simplistic example I know :) . */

if science > 90 then grade = "A"; else if science > 90 then grade = "B"; else grade = "C"; run;

Example of subsetting IF -

data grade; set marks; if science > 90; /* The dataset marks will only contain those records where the value of the variable science was greater than 90. */ run;

Hope this helps. Regards, Anindya

-- Anindya Mozumdar http://www.anindyamozumdar.com


Back to: Top of message | Previous page | Main SAS-L page