BookmarkSubscribeRSS Feed
ybz12003
Rhodochrosite | Level 12

Hello Experts, 

Could anyone help me to simplify the codes?  

 

data want;	
	set have;

	if ulungothsp ^= ' ' and ulungoth =0 then check=1;
	if uhemesp ^= ' ' and uheme =0 then check=2;
	if ugmsp ^= ' ' and ugm =0 then check=3;

	if ulungoth = 1 and ulungothsp = ' ' then check=4;
	if uheme = 1 and uhemesp = ' ' then check=5;
	if ugm = 1 and ugmsp = ' ' then check=6;

run;
	

May use a macro do loop?

%let X1=ulungoth;
%let X2=uheme;
%let X3=ugm;
3 REPLIES 3
ballardw
Super User

Without a deep understanding of what any of those variables may actually mean there isn't much available to simplify.

3 variables and 6 pairs of values to create 6 levels of another variable, not much to work with.

 

I suspect that unless you have multiple groups of variables that need the exact same sort of comparisons that any macro approach will be harder to implement than than what you show. And such a need might be an indicator of poor data structure depending on the following steps using them.

 

Style comment: using the same variables in different positions and separating the code makes it much harder to follow what may be going on.

Consider this:

 

if ulungothsp ^= ' ' and ulungoth =0 then check=1;
if ulungoth = 1 and ulungothsp = ' ' then check=4;

if uhemesp ^= ' ' and uheme =0 then check=2;
if uheme = 1 and uhemesp = ' ' then check=5;

if ugmsp ^= ' ' and ugm =0 then check=3;
if ugm = 1 and ugmsp = ' ' then check=6;

At least that groups similar variables together so it may be possible to see if the code involving any pair of them can be "simplified"

 

Then if a pair is ordered by variable name such as in:

 

if ulungothsp ^= ' ' and ulungoth =0 then check=1;
if ulungothsp = ' '  and ulungoth = 1 then check=4;

you may find something that will simplify. Explicit value comparisons, since we do not know all of your possible values makes that not practical to simplify. It could be make to execute quicker depending on the distribution of missing and non-missing values of ulungothsp. Such as if most of the variable is not missing then

 

if ulungothsp ^= ' ' and ulungoth =0 then check=1;
else if ulungothsp = ' '  and ulungoth = 1 then check=4;

may execute somewhat quicker.

 

IF all of the variables ulungoth, uheme and ugm ONLY take values of 1/0 sometimes you can build things that will use SAS use of 1/0 for true false in numeric calculations with sums and multiplication to do the equivalent of several if/then/else but I won't provide an example that may not apply in this case.

 

And a concern based on your variable names. You appear to have 3 base values and then suffix SP. If the SP had a strong enough meaning it may be that your data could benefit from reshaping were some variable takes the meaning of "is an SP record" and "Is not an SP record" . Maybe.

 

Astounding
PROC Star
I would be less concerned with simplifying the code and more concerned with getting the right answer.

Looking at the criteria for setting CHECK to 1, 2, or 3, it is possible that an observation satisfies the conditions for all 3 values. What is the right outcome in that case?
FreelanceReinh
Jade | Level 19

Hello @ybz12003,

 

Very good points (as usual) from @Astounding and @ballardw.

 


@ballardw wrote:

IF all of the variables ulungoth, uheme and ugm ONLY take values of 1/0 sometimes you can build things that will use SAS use of 1/0 for true false in numeric calculations with sums and multiplication to do the equivalent of several if/then/else ...

I like this idea because it's always fun to "build things" like that. Here is an example which works under the above assumption:

 

data want(drop=x:);
set have;
x1=ulungoth;
x2=uheme;
x3=ugm;
x4=(ulungothsp=' ');
x5=(uhemesp=' ');
x6=(ugmsp=' ');
x=x3*x6*32+x2*x5*16+x1*x4*8+~x3*~x6*4+~x2*~x5*2+~x1*~x4;
if x then check=ceil(log2(x+1));
run;

Whether this is simpler than your series of IF-THEN statements, is a different question ...

 

Here is another variant using your suggested macro variables:


%let X1=ulungoth;
%let X2=uheme;
%let X3=ugm;

data want(drop=c f);	
set have;
retain c .5625;
f=(abs(&x1+(&x1.sp=' ')-c)+2*abs(&x2+(&x2.sp=' ')-c)+4*abs(&x3+(&x3.sp=' ')-c))*8-24.5;
if f then check=ceil(log2(f+1));
run;

 Note that in the formula defining f each of the six variables involved in your IF conditions is used only once (at the cost of mathematical complexity, though).

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 477 views
  • 0 likes
  • 4 in conversation