>,<,= & Character Variables

Reply
Frequent Contributor
Posts: 75

>,<,= & Character Variables

I want to create a new variable Test.

data XXX;

set YYY;

if Apple<Orange then Test=Y;

if Apple=Orange then Test=Equal;

if Apple>Orange then Test=N;

run;

Apple & Orange are Character Variables but have only numbers in them. When I run this code the Test field are all blanks.

How do I change the code?

Thank you

Super User
Posts: 19,770

Re: >,<,= & Character Variables

Test="Y" etc otherwise you're assigning the variable Y to test, not the value.

Frequent Contributor
Posts: 75

Re: >,<,= & Character Variables

data XXX;

set YYY;

if Apple<Orange then Test='Y';

if Apple=Orange then Test='Equal';

if Apple>Orange then Test='N';

run;

Why instead of showing 'Equal' SAS shows 'E'?

Thank you

PROC Star
Posts: 7,468

Re: >,<,= & Character Variables

include a statement that specifies the length of the new variable, e.g.

length test $5;

I can't test it at the moment, but I think declaring a format would also work, e.g.

format test $5.;

Frequent Contributor
Posts: 118

Re: >,<,= & Character Variables

Hi,

Declare the length $5 for test variable. by doing this, it would show "Equal".

Regards;

Uma shanker Saini

Super Contributor
Posts: 282

Re: >,<,= & Character Variables

Hi,

Further to the good advice already given about using the length statement and in answer to your question "Why instead of showing 'Equal' SAS shows 'E'?", the reason is that if you do not define the length of a variable it takes the length of the value that is first assigned to it.

So, for example, the following will set the length of variable var to 1 as it is first assigned a value of "Y" which has a length of 1:

data test1;

  var="Y";

  output;

  var="Equal";

  output;

  var="N";

  output;

run;

Whereas the following will set the length of variable var to 5 as it is first assigned a value of "Equal" which has a length of 5:

data test2;

  var="Equal";

  output;

  var="Y";

  output;

  var="N";

  output;

run;

Using a length statement allows you to set the maximum length you need without thinking about which value you assign to the variable first and it also makes it clear to the human reader.

Regards,

Amir.

Respected Advisor
Posts: 4,173

Re: >,<,= & Character Variables

And adding to what Reeza says: That's why it's always worth to check the log and understand why log messages and warnings get created - eg. the ones about uninitialized variables.

Super User
Super User
Posts: 7,039

Re: >,<,= & Character Variables

If APPLE and ORANGE are character variables and you want to compare their values as if they were the numbers that the character strings represent then you should use the INPUT() function.

data a ;

  length apple orange $32 ;

  input apple orange ;

  char = apple < orange ;

  num = input(apple,32.) < input(orange,32.) ;

  put (_all_) (=);

cards;

1 2

11 2

1E20 200

run;

apple=1 orange=2 char=1 num=1

apple=11 orange=2 char=1 num=0

apple=1E20 orange=200 char=1 num=0

Frequent Contributor
Posts: 75

Re: >,<,= & Character Variables

data a ;

  length apple orange $32 ;

  input apple orange ;

  char = apple < orange ;

  num = input(apple,32.) < input(orange,32.) ;

  put (_all_) (=);

cards;

1 2

11 2

1E20 200

run;

apple=1 orange=2 char=1 num=1

apple=11 orange=2 char=1 num=0

apple=1E20 orange=200 char=1 num=0

Thank you everybody for the helpful comments. But regarding the given code, I don't understand

char = apple < orange ;

  num = input(apple,32.) < input(orange,32.) ;

  put (_all_) (=);

cards;

1 2

11 2

1E20 200

Super User
Super User
Posts: 7,039

Re: >,<,= & Character Variables

char = apple < orange ;

Is an assignment statement. It says to set the variable CHAR to value of the expression on the right of the equal sign.  In this case that is the boolean expression comparing the values of the variables apple and orange.  When the value of apple is less than the value of orange then CHAR will be set to 1 (true) , otherwise it will be set to zero (false).

input(apple,32.) will interpret the character string that is in the variable APPLE using the informat 32.  So anything that looks like a number will be converted to a number.  In this case the input data for APPLE consists of '1' , '11', and '1E20' which will be interpreted by the input() function as the numbers 1, 11 and 1 times 10 to the 20th power (or 1 with 20 zeros after it).

put (_all_) (=);

Says to output the values of the variables listed inside the first set of parentheses using the format operators listed inside the second set of parenthesis. The variable list _ALL_ means to use all of the variables currently defined.  There is only one format listed so it is applied to each variable. The = format operator in a PUT statement means to list the name of the variable followed by an equal sign and then the value of the variable.

CARDS;

Statement says to treat all lines following as data until there is a line with a semi-colon on it.

The rest are the data lines.

PROC Star
Posts: 7,468

Re: >,<,= & Character Variables

Tom was just giving you some examples of why you ought to convert your character fields to numeric fields when doing your comparisons.  Both "char=apple<orange" and "input(apple,32.)<input(orange,32.)" will return zeros or one dependent upon whether the condition to the right of the equal sign is true.  That form results in one of two numbers: 1 if if the condition is true and 0 if it isn't.

Ask a Question
Discussion stats
  • 10 replies
  • 427 views
  • 0 likes
  • 7 in conversation