BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
munitech4u
Quartz | Level 8

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? 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

9 REPLIES 9
JoshB
Quartz | Level 8

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

Reeza
Super User

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?

munitech4u
Quartz | Level 8

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.

Reeza
Super User

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. 

Astounding
PROC Star

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;

munitech4u
Quartz | Level 8

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.

Astounding
PROC Star

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.

munitech4u
Quartz | Level 8
Cool. Seems to be working. Thanks!
munitech4u
Quartz | Level 8
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?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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