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
;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.