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

hi,

 

I have multiple numeric variables that I want to copy all into 1 new variable.  I can do it using PUT.

 

Is there a another way of doing it (preferably that would also work in SQL)?

 

thanks in advance,

 

data have;
  input Id  Rank Count Date;
datalines;
001  01    01     2017
001  02    01     9999
002  01    03     9999
003  01    02     2018
003  02    02     9999
004  01    02     9999
run;

data want;
/*format a $3. b $2. c $2. d $4.; */

set have;
a=PUT(id,best.);
b=PUT(rank,best.);
c=PUT(count,best.);
d=PUT(date,best.);
fin=catx('|',a||b||c||d);

keep fin;


run;
1 ACCEPTED SOLUTION

Accepted Solutions
antonbcristina
SAS Employee

If you use the CATX function, your numeric variables will automatically be converted into character, with leading and trailing spaces removed. However, make sure you mention your variables as individual arguments for the function, not as one long concatenated string. I've included a space delimiter, the first argument to the CATX function, but you could change this to "|" if you need to.

 

And of course, you can use this in PROC SQL as well.

proc sql;
   select catx(' ',id,rank,count,date) as fin
   from have;
quit;

 

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

@brulardwrote:

hi,

 

I have multiple numeric variables that I want to copy all into 1 new variable.  I can do it using PUT.

 

Is there a another way of doing it (preferably that would also work in SQL)?

 

thanks in advance,

 

data have;
  input Id  Rank Count Date;
datalines;
001  01    01     2017
001  02    01     9999
002  01    03     9999
003  01    02     2018
003  02    02     9999
004  01    02     9999
run;

data want;
/*format a $3. b $2. c $2. d $4.; */

set have;
a=PUT(id,best.);
b=PUT(rank,best.);
c=PUT(count,best.);
d=PUT(date,best.);
fin=catx('|',a||b||c||d);

keep fin;


run;

How come there is no delimiter in '|' in your output fin?

antonbcristina
SAS Employee

If you use the CATX function, your numeric variables will automatically be converted into character, with leading and trailing spaces removed. However, make sure you mention your variables as individual arguments for the function, not as one long concatenated string. I've included a space delimiter, the first argument to the CATX function, but you could change this to "|" if you need to.

 

And of course, you can use this in PROC SQL as well.

proc sql;
   select catx(' ',id,rank,count,date) as fin
   from have;
quit;

 

brulard
Pyrite | Level 9

Appreciate the explanation, thank you

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 412 views
  • 1 like
  • 3 in conversation