BookmarkSubscribeRSS Feed
Mikkel_madsen
Obsidian | Level 7

Hi,

 

I have a question (probably an easy one). 

 

How can I add text to a variable? It looks like:

 

ID

27693057

20377519

31679420

02310259

02148619

 

Can I have them to look like these:

 

ID

DM-27693057

DM-20377519

DM-31679420

DM-02310259

DM-02148619

 

A solution could also be to remove the 'DM-' part as I want to join two datasets. The numbers in each ID is identical, but I cant join them right now because of the 'DM-' part. The first variables (only numbers) is in format BEST12. while the variables with DM- is in the format $CHAR12.

 

Thank you!

 

/MM

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

This is pretty easy to do. However, your actual problem seems to be another one. Can you provide a description of the 'join' problem? I think we can solve your actual problem as well 🙂

gamotte
Rhodochrosite | Level 12

Hello,

 

data A_charID;
set A(rename=(ID=Old_ID));

length ID $ 12;

ID=cats('DM-', put(Old_ID,z8.));
run;

data B_numID;
set B(rename=(ID=Old_ID));

ID=input(substr(Old_ID,4), best12.);
run;
ballardw
Super User

 

Join on   a.id = input(substr(b.id,4),12.)

perhaps if meant join in Proc SQL, A would the set with the ID numeric and B the other set with the character ID.

or build the string value with something like Cats('DM-',put(a.id,12. -L) ) = b.id

 

 

PhilC
Rhodochrosite | Level 12

All of the above are viable.  Many ways to skin something.

 

data have_A;
  input ID $8.;
  datalines;
27693057
20377519
31679420
02310259
02148619
;
run;

data have_B;
  input ID $11.;
  datalines;
DM-27693057
DM-20377519
DM-31679420
DM-02310259
DM-02148619
;
run;

data have_B_xformd/view=have_B_xformd;
  set have_B;
  ID_stub=substr(ID,4,8);
run;

proc sql;
  create table join_demo as 
    select a.ID as ID_a,
           b.ID as ID_b
      from have_a as a 
             join 
           have_B_xformd as b
             ON a.ID=b.ID_stub;
quit;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 3936 views
  • 1 like
  • 5 in conversation