Do Loop with IF in Macro

#1
Hi,

I've been struggling with a do loop in SAS and would be grateful for any assistance. This is the data I have:


data have;
input Parameter $ COL1 $ COL2 $;
cards;
Intercept AGE DRINKPATTERN
DRINKPATTERN AGE DRINKPATTERN
DRINKPATTERN AGE DRINKPATTERN
DRINKPATTERN AGE DRINKPATTERN
DRINKPATTERN AGE DRINKPATTERN
FISHLASTWEEK AGE DRINKPATTERN
AGE AGE DRINKPATTERN AGE DRINKPATTERN
AGE AGE DRINKPATTERN AGE DRINKPATTERN
AGE AGE DRINKPATTERN AGE DRINKPATTERN
FISHLASTWEEK*AGE AGE DRINKPATTERN
FISHLASTWEEK*AGE AGE DRINKPATTERN
FISHLASTWEEK*AGE AGE DRINKPATTERN
;
run;

I want to assess whether the string values contained in the variables COL1 or COL2 are evident in the field Parameter and if so assign a value of 2 to the variable TYPE.
The macro I created is below. The problem I am having is that the if statement does not seem to generate and the mprint option highlights this. Any assistance
is greatly appreaciated,

thanks,

%macro ClassIdentifier(value);
data want;
set have;
TYPE = 1;
%do i=1 %to &Value;
%let Varname = COL&i;
%if %index(Parameter, &Varname ) NE 0 %then TYPE = 2;
run;
%end;
%mend;


options mprint;
%ClassIdentifier(2)
 

Mean Joe

TS Contributor
#2
It's a simple change, it looks like:

Code:
%macro ClassIdentifier(value);
data want;
set have;
TYPE = 1;
%do i=1 %to &Value;
%let Varname = COL&i;
%put &Varname;
if index(Parameter, &Varname ) NE 0 then TYPE = 2; %*see that you only need to drop the percent symbols here;
%end;
run; %*I think you want to move the ending run statement outside of the macro do loop;
%mend;
When you use the macro version of %if ... %then ..., you are saying you want code to be written only under certain conditions.
For your use, you actually want the code to be written, it's just you want to write it one time instead of several times. Thus, use the normal version of if ... then ...
 
#3
It's a simple change, it looks like:

Code:
%macro ClassIdentifier(value);
data want;
set have;
TYPE = 1;
%do i=1 %to &Value;
%let Varname = COL&i;
%put &Varname;
if index(Parameter, &Varname ) NE 0 then TYPE = 2; %*see that you only need to drop the percent symbols here;
%end;
run; %*I think you want to move the ending run statement outside of the macro do loop;
%mend;
When you use the macro version of %if ... %then ..., you are saying you want code to be written only under certain conditions.
For your use, you actually want the code to be written, it's just you want to write it one time instead of several times. Thus, use the normal version of if ... then ...
Wow, its always a small thing! Thanks very much for your help, I appreciate it
 
#4
I'd like to overlay a histogram and plot of regression predictions with confidence bands, such as the figure I've attached (from Rayman MP Lancet. 2012 Mar 31;379(9822):1256-68.). Does anyone know how to make a similar figure in SAS? I've tried proc sgplot and proc sgpanel, but they both give an error indicating that the plot types are not compatible.


_________________
http://leather4sure.biz
 
Last edited: