Hi everyone,
I am working on two character variables where I need to do the following with:
First: add double quotation marks to the strings of both of them, for example:
var1 : Q11.0 Q11.1 to become "Q11.0" "Q11.1"
var2: Q110 Q111 to become "Q110" "Q111"
Second: Join each line strings from both variables by commas, for example:
var_all: "Q11.0", "Q110"
"Q11.1" , "Q111"
Is there a one-statement function in SAS that can give me the attempted result?
Thanks so much for your helpful tips in advance!
Treeva
@Reeza wrote:
The QUOTE() function will enclose the value in quotes. The CATX() will combine them. It does depend a bit on how your data is structured but something like the following:
Y = catx(', ', quote(var1), quote(var2));
And depending on the assigned length and actual values of the variables you may need to remove trailing blanks before using the QUOTE function:
Y = catx(', ', quote(strip(var1)), quote(strip(var2)) );
Please post test data in the form of a datastep in future. As such this is not tested:
data want; set have; length want $2000; want=cat('"',tranwrd(var1," ",'","'),'"'," ",'"',tranwrd(var2," ",'","'),'"'"); run;
So effectively replace the space in each variable with quote comma quote, and then cat this with a quote before and after.
Thanks RW2 for your prompt response!
I'll make sure in the future to post a test data in the future, so it can be tested.
I followed your instructions regarding doing the replacement according to my own case, but unfortunately it didn't work! probably it was my fault!
Appreciated
@Reeza wrote:
The QUOTE() function will enclose the value in quotes. The CATX() will combine them. It does depend a bit on how your data is structured but something like the following:
Y = catx(', ', quote(var1), quote(var2));
And depending on the assigned length and actual values of the variables you may need to remove trailing blanks before using the QUOTE function:
Y = catx(', ', quote(strip(var1)), quote(strip(var2)) );
The code perfectly worked!
Thanks a lot, ballardw.
Appreciated!
Thanks, Reeza, for your valuable response!
Your suggested code made it easy for me to digest the next suggestions!
Appreciated!
The best is to try regular expressions but that syntax hurts my eye. If @Ksharp the genius, has the time and wishes to help
Thanks, novinosrin, for stopping by & inviting another user to give a hand here!
Appreciated!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.