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

Dear SAS users,

 

Could you please help me how to optimize or to paraphrase this code into more elegant. I make some code using CATX function. The goal is i want to have three different option in result variable. 

 

Thanks

 

Waldorf

 

 

DATA test1;
SET table_005_test_dec;
KEEP aval lbnrind lbclsig test_dec;
RUN;

DATA test2;
SET test1;
IF NOT MISSING(aval) AND CMISS(lbnrind,lbclsig) = 0 THEN DO;
result = CATX(' ', PUT(ROUND(aval, test_dec), best.), '('!!STRIP(SUBSTR(lbnrind, 1, 1))!!')', '('!!STRIP(lbclsig)!!')');
END;
IF NOT MISSING(aval) AND CMISS(lbclsig) = 0 AND CMISS(lbnrind) = 1 THEN DO;
result = CATX(' ', PUT(ROUND(aval, test_dec), best.), '('!!STRIP(lbclsig)!!')');
END;
IF NOT MISSING(aval) AND CMISS(lbclsig) = 1 AND CMISS(lbnrind) = 0 THEN DO;
result = CATX(' ', PUT(ROUND(aval, test_dec), best.), '('!!STRIP(SUBSTR(lbnrind, 1, 1))!!')');
END;
RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you are using the !! in this to concatenate you might consider nesting a cats call

'('!!STRIP(SUBSTR(lbnrind, 1, 1))!!')'

 

cats ( '(',SUBSTR(lbnrind, 1, 1),')' )

may not "optimize" but is less typing and a bit easier to follow. Plus with CATS you shouldn't need the STRIP function call.  You can nest the various CAT functions inside each other just like using the SUBSTR or other functions.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Pull the first condition and its part of the CATX's out into a separate DO/END block:

data test2;
set test1;
if not missing(aval)
then do;
  result = put(round(aval,test_dec),best.);
  if lbnrind ne "" and lbclsig ne ""
  then result = catx(' ',
    result,
    '('!!strip(substr(lbnrind,1,1))!!')',
    '('!!strip(lbclsig)!!')'
  );
  if lbclsig ne "" and lbnrind = ""
  then result = catx(' ', result, '('!!strip(lbclsig)!!')');
  if lbclsig = "" and lbnrind ne ""
  then result = catx(' ',result,'('!!strip(substr(lbnrind,1,1))!!')');
end;
run;

I also replaced the CMISS functions with direct comparisons, as those variables can be assumed to be character anyway; direct comparisons are faster than function calls.

Longbottom_88
Calcite | Level 5
Hi Kurt, Thanks for the answer. Cheers
ballardw
Super User

If you are using the !! in this to concatenate you might consider nesting a cats call

'('!!STRIP(SUBSTR(lbnrind, 1, 1))!!')'

 

cats ( '(',SUBSTR(lbnrind, 1, 1),')' )

may not "optimize" but is less typing and a bit easier to follow. Plus with CATS you shouldn't need the STRIP function call.  You can nest the various CAT functions inside each other just like using the SUBSTR or other functions.

Longbottom_88
Calcite | Level 5
Hi Ballardw, thanks for the answer. Cheers

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 4 replies
  • 1293 views
  • 0 likes
  • 3 in conversation