Dear community,
I need to merge two datasets with the variable ‘Subject’, but ‘Subject’ variable appear to be different type in each dataset as below:
Column Name | Type | Length | Format | Informat |
Subject | Text | 8 | $8. | $8. |
Column Name | Type | Length | Format | Informat |
Subject | Num | 8 | BEST12. | BEST32. |
Could someone tell me how I can convert the type ‘Num’ to ‘Text’ or ‘Text’ to ‘Num’, please.
Thank you
Here you can go ...
data have1;
input subject;
datalines;
1
2
3
;
run;
data have2;
input subject $;
datalines;
1
2
3
;
run;
data have3 (drop=subject);
set have2;
subject1=input(subject,8.);
run;
data want;
merge have1(in=a) have3(in=b rename=(subject1=subject));
by subject;
if a and b;
run;
I use SAS 9, in the dataset view columns, it dose show as 'Text'.
I have tried to use PUT() converts a number to character and INPUT() to convert a character to a number, and rename the variable after, but the merge is not successful, I am wondering if all the length, format and informat properties should also be the same.
@zimcom wrote:
I use SAS 9, in the dataset view columns, it dose show as 'Text'.
I have tried to use PUT() converts a number to character and INPUT() to convert a character to a number, and rename the variable after, but the merge is not successful, I am wondering if all the length, format and informat properties should also be the same.
No, PUT()/INPUT() should work. Post your code and log, you likely have a mistake somewhere.
The best solution is to create the datasets using the right types.
Otherwise it is probably best to convert them both to numbers. There are no formatting issues with numbers.
The problem with converting the number to text is how do you format the numbers. Are they integers? Do they have leading zeros? Leading spaces?
Here you can go ...
data have1;
input subject;
datalines;
1
2
3
;
run;
data have2;
input subject $;
datalines;
1
2
3
;
run;
data have3 (drop=subject);
set have2;
subject1=input(subject,8.);
run;
data want;
merge have1(in=a) have3(in=b rename=(subject1=subject));
by subject;
if a and b;
run;
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.