BookmarkSubscribeRSS Feed
apple
Calcite | Level 5

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

10 REPLIES 10
Reeza
Super User

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

apple
Calcite | Level 5

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

art297
Opal | Level 21

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

umashankersaini
Quartz | Level 8

Hi,

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

Regards;

Uma shanker Saini

Amir
PROC Star

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.

Patrick
Opal | Level 21

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.

Tom
Super User Tom
Super User

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

apple
Calcite | Level 5

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

Tom
Super User Tom
Super User

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.

art297
Opal | Level 21

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10 replies
  • 984 views
  • 0 likes
  • 7 in conversation