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 ;
I have no idea helping to solve the problem - maybe to early. But why do you store sas code in variable at all?
Could CATX(' ',<list of vars and strings>) eventually be the resolution you're after?
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.
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.
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.
Ready to level-up your skills? Choose your own adventure.