data x;
y=1;
run;
data x1;
set x;
if vtype(y)='C' then do;
if y=c bg then x=1;
end;
run;
Why is it throwing error. Since y is numeric, ideally, it should not go into the loop?
OK, I'll try to modify your code as little as possible. I assume all of this is contained within a macro definition, since %IF would give you an error otherwise.
First, after the %DO statement, add as the first step within the loop:
data _null_;
set &dataset;
if vtype(&&varn&i)='N' then call symputx('bin_val', "&&bin&i");
else call symputx('bin_val', "'&&bin&i'");
stop;
run;
Then use &BIN_VAL in your DATA step:
data &output_lib..&out_woe;
set &dataset;
if &&varn&i=&bin_val then &&new_var&i=&&woe&i;
run;
All of this assumes that your program is otherwise correct. That part is up to you.
Syntax is still checked and this looks like a syntax error. What are you trying to do with "bg" there?
I run your code and get the following:
281 if y=c bg then x=1;
--
388
76
ERROR 388-185: Expecting an arithmetic operator.
ERROR 76-322: Syntax error, statement will be ignored.
Your x dataset only has the variable y. What are c, bg supposed to be?
well. Here is the situation.
I want to impute the values based on the numeric and character in an automated macro. Now if it happens to be numeric values I need to put the condition like "If y=1 then z=0;"
If y happens to be a character, I want it to be like "If y="A" then z=0;
Now I have to write these different conditions based on if y is numeric or character. But execute only one, based on whether it is numeric or character.
So condition where it is character and goes to the condition under numeric: if y= a bc it fails. while it should have been if y="a bc" under character condition.
If you have conditional logic like that, its a case where I would recommend going into macro progrramming.
It sounds like you're trying to generalize a process, so also a good indication that macro programming may be appropriate.
The simplest fix would be to create a macro variable holding either 1 or "c bg". For example:
data _null_;
set x;
if vtype(y)='C' then call symput('y_val', '"c bg"');
else call symput('y_val', '1');
stop;
run;
Then use the macro variable later. For example:
data x1;
set x;
if y = &y_val then x=1;
run;
Ok, here is my full code:
data _null_;
set &output_lib..&outdata;
call symput(compress('varn'||_n_),variable_name);
call symput(compress('bin'||_n_),bins);
call symput(compress('woe'||_n_),woe);
call symput(compress('new_var'||_n_),compress(variable_name||"_w"));
run;
proc sql;
select count(*)-1 into: max_obs from &output_lib..&outdata ;quit;
%do i=1 %to &max_obs;
data &output_lib..&out_woe;
set &dataset;
if vtype(&&varn&i)='N' then do;
if &&varn&i=&&bin&i then &&new_var&i=&&woe&i;
end;
else do;
if &&varn&i="&&bin&i" then &&new_var&i=&&woe&i;
end;
run;
%end;
I will be scanning the variable type while setting the dataset. is there a way around this. else I am planning to get the variable type and merge with &output_lib..&outdata to get the variable type and keep the statements within macro condition and not within datastep.
OK, I'll try to modify your code as little as possible. I assume all of this is contained within a macro definition, since %IF would give you an error otherwise.
First, after the %DO statement, add as the first step within the loop:
data _null_;
set &dataset;
if vtype(&&varn&i)='N' then call symputx('bin_val', "&&bin&i");
else call symputx('bin_val', "'&&bin&i'");
stop;
run;
Then use &BIN_VAL in your DATA step:
data &output_lib..&out_woe;
set &dataset;
if &&varn&i=&bin_val then &&new_var&i=&&woe&i;
run;
All of this assumes that your program is otherwise correct. That part is up to you.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.