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

 

The following code works when macro var2 is defined as an non-missing existing variable name. But when I only want var1 and leave var2 empty (basically let the if condition fail and don't do the var2 transformation), it fails. How should I correct it?

 

%let var1 = weight;
%let var2 = age; 
/*%let var2 = ;*/ data want; set have; &var1._1 = &var1 / 2; if "%str(&var2.)" ^= "" then do; &var2._2 = &var2. / 2; end; run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

I don't think you can generate conditional code without a macro, such as:

 

%macro a(dsn, var1, var2);
data &dsn;
set sashelp.class;
&var1._1 = &var1 / 2;
%if %length(&var2) > 0 %then &var2._2 = &var2. / 2 ;;
run;
%mend;

%a(want1, weight, age);
%a(want2, weight,);

 

PG

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

I don't think you can generate conditional code without a macro, such as:

 

%macro a(dsn, var1, var2);
data &dsn;
set sashelp.class;
&var1._1 = &var1 / 2;
%if %length(&var2) > 0 %then &var2._2 = &var2. / 2 ;;
run;
%mend;

%a(want1, weight, age);
%a(want2, weight,);

 

PG
Tom
Super User Tom
Super User

@apolitical wrote:

 

The following code works when macro var2 is defined as an non-missing existing variable name. But when I only want var1 and leave var2 empty (basically let the if condition fail and don't do the var2 transformation), it fails. How should I correct it?

 

%let var1 = weight;
%let var2 = age; 
/*%let var2 = ;*/ data want; set have; &var1._1 = &var1 / 2; if "%str(&var2.)" ^= "" then do; &var2._2 = &var2. / 2; end; run;

 


Of course it fails.  Just replace the macro variable references with their values and look at the statements that result. 

data want;
set have;
weight_1 = weight / 2;
if "" ^= "" then do;
	_2 =  / 2;
end;
run;

You could get the SAS code to run if you changed it like this

if "%str(&var2.)" ^= "" then do;
	&var2._2 = (&var2.+0) / 2;
end;

Then it would generate valid SAS code.

if "" ^= "" then do;
	_2 =  (+0)/ 2;
end;

Of course I am not sure why you would want to generate the variable _2 , but at least the step will run.

 

Tom
Super User Tom
Super User

Since you seem to be doing the same thing to each variable why not just use ARRAY processing?

Here is code that takes a list of variables and creates new variables by adding _2 to the variable name and dividing the value of the corresponding old value by 2.

%let varlist = weight age;

data want;
  set sashelp.class;
  array old &varlist ;
  array new %sysfunc(tranwrd(%sysfunc(compbl(&varlist)),%str( ),%str(_2 )))_2;
  do _n_=1 to dim(old);
    new(_n_)=old(_n_)/2 ;
  end;
run;

image.png

Tom
Super User Tom
Super User

If you are using SAS 9.4M5 then you can use a %IF block in open code.

data want;
  set have;
  &var1._1 = &var1 / 2;
%if %length(&var2) %then %do;
  &var2._2 = &var2. / 2;
%end;
run;
apolitical
Obsidian | Level 7

Thanks everyone! 

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
  • 5 replies
  • 11432 views
  • 1 like
  • 3 in conversation