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


Hello!,

is there a sas character function to combine columns having different variable lengths?

I would combine two tables using patient_id . However, the lengths are different . I would like to substitute '0'  to match it up with other table 

For example,   patient_id  137 in patient table and patient source_no in the other table  = '0000000137'

Please advise,

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
RaviKommuri
Fluorite | Level 6

Hi Avatar,

This code may be useful for your requirement.

Anybody can correct me if any errors in my code....!!!

DATA HAVE1;                         

INPUT patid $ data1 $;             

DATALINES;                         

111 A                               

112 B                               

;                                   

                                    

DATA HAVE2;                         

INPUT patid $ data2 $;             

DATALINES;                         

000111 C                            

000112 D                            

;                                   

                                    

DATA HAVE1;                         

SET HAVE1;                         

PATIDN = INPUT(PATID,9.);          

RUN;                                

                                    

DATA HAVE2;                         

SET HAVE2;                         

PATIDN = INPUT(PATID,9.);          

RUN;                                

                                    

PROC SORT DATA=HAVE1; BY PATIDN; RUN;

PROC SORT DATA=HAVE2; BY PATIDN; RUN;

                                    

DATA WANT;                          

MERGE HAVE1 HAVE2;

  BY PATIDN;      

RUN;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You can do it various ways:

proc sql;

     create table WANT as

     select     put(input(PATIENT_ID,$10.),z10.) as PATIENT_ID

     from        TABLE1

     union all

     select     PATIENT_ID

     from          TABLE2;

quit;

data want;

     set table1 (in=a) table2 (in=b);

     if a then patient_id=repeat('0',10 - length(strip(patient_id)))||strip(patient_id);

run;

probably half a dozen other methods.

stat_sas
Ammonite | Level 13

Hi,

How about to make patient_source_no numeric? In this way you can eliminate 0s even if they vary within each observation and can join tables.

data have;

input patient_source_no $20.;

patient_id=input(patient_source_no,best.);

datalines;

0000000137

;

RaviKommuri
Fluorite | Level 6

Hi Avatar,

This code may be useful for your requirement.

Anybody can correct me if any errors in my code....!!!

DATA HAVE1;                         

INPUT patid $ data1 $;             

DATALINES;                         

111 A                               

112 B                               

;                                   

                                    

DATA HAVE2;                         

INPUT patid $ data2 $;             

DATALINES;                         

000111 C                            

000112 D                            

;                                   

                                    

DATA HAVE1;                         

SET HAVE1;                         

PATIDN = INPUT(PATID,9.);          

RUN;                                

                                    

DATA HAVE2;                         

SET HAVE2;                         

PATIDN = INPUT(PATID,9.);          

RUN;                                

                                    

PROC SORT DATA=HAVE1; BY PATIDN; RUN;

PROC SORT DATA=HAVE2; BY PATIDN; RUN;

                                    

DATA WANT;                          

MERGE HAVE1 HAVE2;

  BY PATIDN;      

RUN;

avatar
Fluorite | Level 6

I will try doing in Proc sql.

Thanks

RaviKommuri
Fluorite | Level 6

Yes, Still you can use the above code with

INPUT patid $10.  data1 $; 


and


PATIDN = INPUT(PATID,10.);


Hope it will work for you..

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 5 replies
  • 1903 views
  • 1 like
  • 4 in conversation