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
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;
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.
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
;
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;
I will try doing in Proc sql.
Thanks
Yes, Still you can use the above code with
INPUT patid $10. data1 $;
and
PATIDN = INPUT(PATID,10.);
Hope it will work for you..
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.