BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zimcom
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
singhsahab
Lapis Lazuli | Level 10

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;

View solution in original post

6 REPLIES 6
Reeza
Super User
What software are you using? SAS has char and num, not ’text’.

If you’re using SAS PUT() converts a number to character and INPUT() is used to convert a character to a number. SAS doesn’t allow for the variable to change types so you need to Rename it as well.
zimcom
Pyrite | Level 9

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.  

convert.JPG

Reeza
Super User

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

convert.JPG


No, PUT()/INPUT()  should work. Post your code and log, you likely have a mistake somewhere.

Tom
Super User Tom
Super User

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?

singhsahab
Lapis Lazuli | Level 10

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;
zimcom
Pyrite | Level 9

@singhsahab

@Reeza

@Tom

 

Thank you all and have a great day!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 6 replies
  • 5255 views
  • 0 likes
  • 4 in conversation