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..

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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