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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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)) );

View solution in original post

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Treeva
Calcite | Level 5

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
Super User
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));

ballardw
Super User

@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)) );

Treeva
Calcite | Level 5

 

The code perfectly worked!

 

Thanks a lot, ballardw.

 

Appreciated!

Treeva
Calcite | Level 5

Thanks, Reeza, for your valuable response!

 

Your suggested code made it easy for me to digest the next suggestions!

 

Appreciated!

novinosrin
Tourmaline | Level 20

The best is to try regular expressions but that syntax hurts my eye. If @Ksharp the genius, has the time and wishes to help

Treeva
Calcite | Level 5

 

Thanks, novinosrin, for stopping by & inviting another user to give a hand here!

Appreciated!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 1950 views
  • 4 likes
  • 5 in conversation