Why this code is failing?

Accepted Solution Solved
Reply
Regular Contributor
Posts: 188
Accepted Solution

Why this code is failing?

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? 


Accepted Solutions
Solution
‎04-13-2016 01:41 PM
Super User
Posts: 5,069

Re: Why this code is failing?

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.

View solution in original post


All Replies
Contributor
Posts: 55

Re: Why this code is failing?

Syntax is still checked and this looks like a syntax error. What are you trying to do with "bg" there?

Super User
Posts: 17,730

Re: Why this code is failing?

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?

Regular Contributor
Posts: 188

Re: Why this code is failing?

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.

Super User
Posts: 17,730

Re: Why this code is failing?

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. 

Super User
Posts: 5,069

Re: Why this code is failing?

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;

Regular Contributor
Posts: 188

Re: Why this code is failing?

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.

Solution
‎04-13-2016 01:41 PM
Super User
Posts: 5,069

Re: Why this code is failing?

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.

Regular Contributor
Posts: 188

Re: Why this code is failing?

Cool. Seems to be working. Thanks!
Regular Contributor
Posts: 188

Re: Why this code is failing?

Is there a way, I can optimize it? Because I here I am setting the dataset for each variable. Can I avoid that and let the code write only the if conditions and then run?
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 464 views
  • 0 likes
  • 4 in conversation