DATA Step, Macro, Functions and more

If '>' symbol we need to add +0.01, if '<' we need to substract -0.01?

Reply
Frequent Contributor
Posts: 86

If '>' symbol we need to add +0.01, if '<' we need to substract -0.01?

data x;
input a $10.;
cards;
23
12
0.02
<10
>40
<12
<0
run;

 

If '>' symbol we need to add +0.01, if  '<' we need to substract -0.01. I have return a code can some one write more efficient then it.... 

 

My code::


data x1;
set x;
if findc(a,'>')>0 then c=input(compress(a,'><'),best.)+0.01;
else if findc(a,'<')>0 then c=input(compress(a,'><'),best.)-0.01;
else if findc(a,'><')=0 then c=input(a,best.);
run;

 

Trusted Advisor
Posts: 1,311

Re: If '>' symbol we need to add +0.01, if '<' we need to substract -0.01?

Posted in reply to rajeshalwayswel

If the symbols '<' and '>' only occurs as the first character then you can use the =: operator to make your code more compact, but not more efficient:

 

data x1;
  set x;
  if      a=:'<' then c=input(compress(a,'><'),best.)+0.01;
  else if a=:'>' then c=input(compress(a,'><'),best.)-0.01;
  else                c=input(a,best.);
run;
Frequent Contributor
Posts: 86

Re: If '>' symbol we need to add +0.01, if '<' we need to substract -0.01?

what the ":" will do....
Frequent Contributor
Posts: 86

Re: If '>' symbol we need to add +0.01, if '<' we need to substract -0.01?

Posted in reply to rajeshalwayswel
 
Frequent Contributor
Posts: 86

Re: If '>' symbol we need to add +0.01, if '<' we need to substract -0.01?

Posted in reply to rajeshalwayswel
I understand it.....Thank you.... Smiley Happy
Frequent Contributor
Posts: 86

Re: If '>' symbol we need to add +0.01, if '<' we need to substract -0.01?

Posted in reply to rajeshalwayswel
Is another like ":" way if '<>' end's at last..
Esteemed Advisor
Posts: 5,482

Re: If '>' symbol we need to add +0.01, if '<' we need to substract -0.01?

Posted in reply to rajeshalwayswel

An alternative which is easier to use once it is defined is a custom informat. Example:

 


/* Informat function to read numbers possibly prefixed with a
   lower than and greater than character.
   The number is read with the best. informat.
   The value returned is reduced (<) or increased (>) by 0.01, 
   if required */ 
proc fcmp outlib=work.fcmp.format;
function bestltgt(text $);
if first(text) = "<" then return (input(substr(text,2), best.) - 0.01);
if first(text) = ">" then return (input(substr(text,2), best.) + 0.01);
else if anyalpha(text) then return(.);
else return (input(text, best.));
endsub;
run;

options cmplib=(work.fcmp);

proc format;
invalue bestltgt(default=32) 
OTHER = [bestltgt()];
run;

data x;
input a bestltgt10.;
cards;
23
12
0.02
<10
>40
<12
<0
;
PG
Frequent Contributor
Posts: 86

Re: If '>' symbol we need to add +0.01, if '<' we need to substract -0.01?

It's tricky too...but new to learn from it...Thank you...
Ask a Question
Discussion stats
  • 7 replies
  • 129 views
  • 2 likes
  • 3 in conversation