Fluorite | Level 6

## Concatnation of character and number into one variable

Hi, I need to create a new variable from three variables. example

a = 'S'  -->   of size 1 character

b = 'XYZ' --->  of size 3 character. Here value of b can be  'XY'  or 'X'  This means  size of variable is 3 but can have 2 character or 1 character value.

c = 11 ---->  this is 2 digit numeric  and can have values from 1 to 20  This means size of variable is 2 but can have 1 digit or 2 digit.

I need to combined all three variable and  create new variable  Z  of size \$7.

So as per Example

if

a = 'S'   b = 'XY'  c = 1   then  Z = 'SXY 01'  ( 1 space in between)

a = 'S'   b = 'X'    c = 5   then  Z = 'SX  05'  ( two spaces in between)

I tried CAT, CATX, CATX  functions but not getting results.

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Concatnation of character and number into one variable

Forget APP functions. How about CAT?

``````data have;
length a \$1 b \$3 ;
a = 'S'  ; b ='XY' ; c = 1;
output;
a = 'S' ;  b = 'X'   ; c = 5 ;
output;
run;

data want;
set have;
length _c \$2 z \$6;
_c=put(c,z2.);
z=cat(a,b,_c);
run;
``````

8 REPLIES 8
Tourmaline | Level 20

## Re: Concatnation of character and number into one variable

HI @sgarg

Z  of size \$7. or size \$6(3+2+1)?

Tourmaline | Level 20

## Re: Concatnation of character and number into one variable

``````
data have;
length a \$1 b \$3 ;
a = 'S'  ; b ='XY' ; c = 1;
output;
a = 'S' ;  b = 'X'   ; c = 5 ;
output;
run;

data want;
set have;
length _c \$2 z \$6;
_c=put(c,z2.);
run;``````

NOTES:

1. We do not know your real data whether those variables are grouped contiguous. So the need for peekc *3 times. If it is contiguous, just extracting the left most var's addr of the leftmost byte with vlength combination of a,b,c will get the required in one shot.

2. APP functions use is highly dependent on knowing data first.

3. While for the description that you gave, that answered.

4. Use the long brethren if  you are working on 64 bit machine i.e substitute with peekclong, addrlong accordingly

5. If you are not used to APP data management functions, ignore my post

Fluorite | Level 6

## Re: Concatnation of character and number into one variable

@novinosrin  Thanks for your reply. I am using SAS Enterprise guide and got error see NOTE section below.

27         data have;
28         length a \$1 b \$3 ;
29         a = 'S'  ; b ='XY' ; c = 1;
30         output;
31         a = 'S' ;  b = 'X'   ; c = 5 ;
32         output;
33         run;

NOTE: The data set WORK.HAVE has 2 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time           0.00 seconds
cpu time            0.00 seconds

34
35         data want;
36         set have;
37         length _c \$2 z \$6;
38         _c=put(c,z2.);
40         run;

NOTE: PEEKC function cannot be used on this platform. Use PEEKCLONG instead.
NOTE: There were 2 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 2 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time           0.00 seconds
cpu time            0.00 seconds

Fluorite | Level 6

## Re: Concatnation of character and number into one variable

@novinosrin    Correct Z is of size  \$6.  lol.. Thanks.

Tourmaline | Level 20

## Re: Concatnation of character and number into one variable

Forget APP functions. How about CAT?

``````data have;
length a \$1 b \$3 ;
a = 'S'  ; b ='XY' ; c = 1;
output;
a = 'S' ;  b = 'X'   ; c = 5 ;
output;
run;

data want;
set have;
length _c \$2 z \$6;
_c=put(c,z2.);
z=cat(a,b,_c);
run;
``````

Fluorite | Level 6

## Re: Concatnation of character and number into one variable

@novinosrin  Yes it worked. Thank you Thank you so much..

Quartz | Level 8

## Re: Concatnation of character and number into one variable

@sgarg  Mark the post as answered having acknowledged the help. That completes and closes it.

Tourmaline | Level 20

## Re: Concatnation of character and number into one variable

Finally for fun and completeness,

My Notes above point 4. emphasizes LONG APP' for the 64 bit machine, therefore the code changes

``````
data have;
length a \$1 b \$3 ;
a = 'S'  ; b ='XY' ; c = 1;
output;
a = 'S' ;  b = 'X'   ; c = 5 ;
output;
run;

data want;
set have;
length _c \$2 z \$6;
_c=put(c,z2.);