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..
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.