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

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

 

View solution in original post

8 REPLIES 8
novinosrin
Tourmaline | Level 20

HI @sgarg

 

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

 

 

novinosrin
Tourmaline | Level 20

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

sgarg
Fluorite | Level 6

@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
     

sgarg
Fluorite | Level 6

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

novinosrin
Tourmaline | Level 20

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;

 

sgarg
Fluorite | Level 6

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

Allaluiah
Quartz | Level 8

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

novinosrin
Tourmaline | Level 20

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;

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
  • 8 replies
  • 852 views
  • 0 likes
  • 3 in conversation