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

Hi, 

 

I am having problem in passing fomatted values in if condition. 

I have below TRTP values in my count dataset:

Capture 1.PNG

 

And I want to create another variable by using below calculation.

data count1;
	set count;
	if TRTP eq "A" then c2=count/Count1*100;
run;

but I am getting missing values for c2 variable, but log is clear.

 

Is there anything I am missing there?

 

Thanks,

Adithya

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Your code compares TRTP to "A", but if TRTP is formatted, it may have underlying values different than what you see.

 

Any such comparison

 

if TRTP eq "A"

uses the unformatted values, not the formatted values. So the comparison would fail, even if it LOOKS LIKE your variable TRTP has value "A".

 

So just to make up an example, if the format $TRT1. is something like this:

 

proc format;
    value $trt "A","B","Q","T"="A";
run;

Then the comparison you would want to do is

 

if TRTP in ("A","B","Q","T")

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Your code compares TRTP to "A", but if TRTP is formatted, it may have underlying values different than what you see.

 

Any such comparison

 

if TRTP eq "A"

uses the unformatted values, not the formatted values. So the comparison would fail, even if it LOOKS LIKE your variable TRTP has value "A".

 

So just to make up an example, if the format $TRT1. is something like this:

 

proc format;
    value $trt "A","B","Q","T"="A";
run;

Then the comparison you would want to do is

 

if TRTP in ("A","B","Q","T")

 

--
Paige Miller
ballardw
Super User

To compare a value to a formatted value explicitly use the format:

 

If put(trtp,$trt1.) = 'A' then <whatever>;

FreelanceReinh
Jade | Level 19
@ballardw wrote:

To compare a value to a formatted value explicitly use the format:

 

If put(trtp,$trt1.) = 'A' then <whatever>;


 

Another option is to use the format implicitly:

if vvalue(trtp)='A' then ...

 (See VVALUE function. Be careful, however, when using this for unformatted numeric variables. In that case the default format BEST12. will apply.)

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 2574 views
  • 3 likes
  • 4 in conversation