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.
Please Advice what i can do...
Thank you.
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;
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=peekc(addr(a),1)||peekc(addr(b),3)||peekc(addr(_c),2);
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
@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.);
39 z=peekc(addr(a),1)||peekc(addr(b),3)||peekc(addr(_c),2);
40 run;
NOTE: ADDR function cannot be used on this platform. Use ADDRLONG instead.
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
@novinosrin Correct Z is of size $6. lol.. Thanks.
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;
@novinosrin Yes it worked. Thank you Thank you so much..
@sgarg Mark the post as answered having acknowledged the help. That completes and closes it.
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.);
z=peekclong(addrlong(a),1)||peekclong(addrlong(b),3)||peekclong(addrlong(_c),2);
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: