DATA Step, Macro, Functions and more

Using IF condition while using PUT function

Reply
Contributor
Posts: 45

Using IF condition while using PUT function

Hello everyone,

 

i Have dataset 

ID Sex Age Date Height Weight Fee
2458 M 27 1 72 168 85.2
2462 F 34 3 66 152 124.8
2501 F 31 17 61 123 149.75
2523 F 43 31 63 137 149.75
2539 M 51 4 71 158 124.8
2544 M 29 6 76 193 124.8
2552 F 32 9 67 151 149.75
2555 M 35 13 70 173 149.75
2563 M 34 22 73 154 124.8
2568 F 49 27 64 172 124.8
2571 F 44 19 66 140 149.75
2572 F 28 17 62 118 85.2
2574 M 30 6 69 147 149.75
2575 F 40 8 69 163 124.8
2578 M 47 5 72 173 124.8
2579 M 60 22 71 191 149.75
2584 F 43 29 65 123 124.8
2586 M 25 23 75 188 85.2
2588 F 22 20 63 139 85.2
2589 F 41 16 67 141 149.75
2595 M 54 7 71 183 149.75

 

Lets say if i run a code like

 


data a;
set sasuser.admit;
per=age/fee;

form=put(per,percent9.2);

if form gt "30%";

run;

 

 

It will not give any observation.

 

I want to know how we can use the If condition on the format we used in PUT function.

 

Thanks in advance.

Super User
Posts: 22,857

Re: Using IF condition while using PUT function

Posted in reply to shivamarrora0

Yes, but the IF statement needs to be valid.

 

In this case you've converted it to a character variable, so searching for values greater than 30% in character variables may not be what you want. Is this what you want by chance?

 

data a;
set sasuser.admit;
per=age/fee;
format per percent12.2;
if per gt 0.3;
run;
Contributor
Posts: 45

Re: Using IF condition while using PUT function

i want to know how i can use the filter  after applying PUT function

Super User
Posts: 6,542

Re: Using IF condition while using PUT function

[ Edited ]
Posted in reply to shivamarrora0

You already got an answer from @Reeza about what you should be doing.

 

Here's why what you did isn't returning any observations.

 

When comparing character strings, SAS does not make a numeric comparison.  For example:

 

"12345" < "30"  because the first character of "12345" is less than the first character of "30"

 

"5" > "30" because the first character of "5" is greater than the first character of "30"

 

SAS looks at the first character only, to determine which string is greater.  If the first characters are equal, only then does SAS move on to the second character in order to break the tie.

 

That's half the story.  But there's still the question of why your program returned no observations at all.  That's a result of using the percent format.  The percent format always generates character strings with a leading and a trailing blank.  The output from the percent function is always less than "30%" because blank is less than "3".

 

To answer your question about can you do this?  The program you ran didn't generate any error messages.  It just gave the wrong answer.  Expect that to be the result.

Super User
Posts: 13,066

Re: Using IF condition while using PUT function

Posted in reply to Astounding

Actually the leading and trailing blanks are to allow for the () when the value is negative:

data example;
   x= -0.12345;
   y = put(x,percent12.3);
run;

Since many of us don't generate negative percentages it isn't obvious.

 

You can get the results to left justify with

y=put(x,percent12.3 -L);

 

But as @Astounding points out greater than or less than comparisons with character values are generally a poor idea.

Frequent Contributor
Posts: 113

Re: Using IF condition while using PUT function

[ Edited ]
Posted in reply to shivamarrora0

Try this.....

 

/*Build HAVE table */
data HAVE; do i=1 to 10 BY 0.01; output; end; run;

data WANT;
set HAVE(where=(i/10 gt 0.3));
FORM=put(i/10,percent9.2);
run;

 

 

Hope this helps.

Ask a Question
Discussion stats
  • 5 replies
  • 130 views
  • 3 likes
  • 5 in conversation