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