BookmarkSubscribeRSS Feed
keen_sas
Quartz | Level 8

I have few dynamic conditions provided as below.As per the below example if i use cat function, we have lot of spaces in the variable QDT2 and STDY2,if catt/cats are used all the trailing and leading spaces will be removed respectively, but i want to retain spaces present before and after the text present in the quotes  . For example if catt/cats are used spaces will be removed(" (Study day ") after day and also before parenthesis (study day. The ideal way is to use cat function or pipe symbol and to add strip to variables as below, but the challenge is to identifying variables to add strip function.How to identify and add strip only to the variables without affecting the text in quotes.

 


data have;
length 2000;
have="if REF ='999' and not missing(DY2) then DATE = cat(QDT2 ,' (Study day ' ,STDY2 ,')' );";output;
have="if REF = 'UNKNOWN' then EVENT = '-';";output;
have="if EVENT = TERM then DECODE=TERM;";output;
run ;

data want;
want="if REF ='999' and not missing(DY2) then DATE = cat(strip(QDT2) ,' (Study day ' ,strip(STDY2) ,')' );";output;
want="if REF = 'UNKNOWN' then EVENT = '-';";output;
want="if EVENT = TERM then DECODE=strip(TERM);";output;
run ;

4 REPLIES 4
andreas_lds
Jade | Level 19

I have no idea helping to solve the problem - maybe to early. But why do you store sas code in variable at all?

Patrick
Opal | Level 21

Could CATX(' ',<list of vars and strings>) eventually be the resolution you're after? 

keen_sas
Quartz | Level 8

Even Catx also removes the leading and trailing blanks from the variable and also from the string or sub string as mentioned in the example. Reason to include the SAS code in variables is part of the automation , writing logical conditions in the excel sheet and it has to executed.  

Patrick
Opal | Level 21

The very first element in the CATX() function defines the delimiter string when concatenating strings. If you chose a blank there then yes, CATX() will first strip trailing and leading blanks from the source string/variable but then add a single blank between the strings it concatenates. That's often what's needed.

If you need some combination of how things get concatenated then don't use a single function but a combination of functions (cat, cats, catx,strip) or even just the 'stringA'||'stringB'. 

You really can get whatever you need by choosing the right combination.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 1008 views
  • 0 likes
  • 3 in conversation