DATA Step, Macro, Functions and more

SAS Character Variable comparision

Occasional Contributor
Posts: 14

SAS Character Variable comparision

I have two datasets.I have to create an output dataset from these two with the condition that var2 of dataset1 should match the var2 of dataset2. Only concern is Var2 is $9 here and $32 in the second dataset.


var1 var2($9)
1 0000283
2 731823
3 1073388
4 12

varX var2($32)
1 000002837
2 000731828
3 001073388
4 000161499

How to get the result as :

var1 varx
3 3

I tried left, trim, input and put fuctions but i am getting absurd results. Thats becaue when I convert the character to numeric for comparasion purpose, the values wiht more than 8 characters are trimmed and so wrong matching ouptut is generate. Is there any way I can compare this two variable.

Note : Size of both vairbles are $9 and $32 and the values in this are starting from 1 character to any.

Thanks for your help...
Super Contributor
Posts: 474

Re: SAS Character Variable comparision

Sorry, I initially misunderstood the problem.

Here's my modified post.

Create a view over the each dataset and convert the variable (alpha) to an numeric equivalent.
proc sql noprint;
create view X1 as
select var1, input(var2,best.) as VAR2 from INDATA1;

proc sql noprint;
create view X2 as
select varX, input(var2,best.) as VAR2 from INDATA2;
then merge the views into a single dataset.

Should work.

Cheers from Portugal

Daniel Santos @
Occasional Contributor
Posts: 14

Re: SAS Character Variable comparision

Thanks Daniel..This works...Can you please tell me how to convert a character variable to numeric vairable if the character variable is more that 8 characters...

Ex. '123456789' or '1234567890' etc
Posts: 74

Re: SAS Character Variable comparision

if only problem is leading 0 then you may use substr and not having to mess up with type switch from char to num.
Super Contributor
Super Contributor
Posts: 3,174

Re: SAS Character Variable comparision

Conversion can be done with the DATA step assignment statement, using the INPUT function and the appropriate INFORMAT/length - example below:

17 data _null_;
18 x = input('123456789012345',15.);
19 put x= comma20. / x= best.;
20 run;

NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

To append leading zeros, use the Znn. SAS format, and if you must convert to a character type variable, use the SAS PUT function with Znn. format in an assignment statement.

Scott Barry
SBBWorks, Inc.
Occasional Contributor
Posts: 14

Re: SAS Character Variable comparision

Thank you all, it worked...
Ask a Question
Discussion stats
  • 5 replies
  • 4 in conversation