Desktop productivity for business analysts and programmers

SAS Character Function

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

SAS Character Function


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


Accepted Solutions
Solution
‎08-08-2014 10:08 AM
Contributor
Posts: 45

Re: SAS Character Function

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


All Replies
Super User
Super User
Posts: 7,721

Re: SAS Character Function

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.

Trusted Advisor
Posts: 1,228

Re: SAS Character Function

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

;

Solution
‎08-08-2014 10:08 AM
Contributor
Posts: 45

Re: SAS Character Function

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;

Contributor
Posts: 66

Re: SAS Character Function

I will try doing in Proc sql.

Thanks

Contributor
Posts: 45

Re: SAS Character Function

Yes, Still you can use the above code with

INPUT patid $10.  data1 $; 


and


PATIDN = INPUT(PATID,10.);


Hope it will work for you..

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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