BookmarkSubscribeRSS Feed
rajeshalwayswel
Pyrite | Level 9

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;

 

7 REPLIES 7
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
rajeshalwayswel
Pyrite | Level 9
I understand it.....Thank you.... 🙂
rajeshalwayswel
Pyrite | Level 9
Is another like ":" way if '<>' end's at last..
PGStats
Opal | Level 21

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
rajeshalwayswel
Pyrite | Level 9
It's tricky too...but new to learn from it...Thank you...

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand in the Innovate Hub.

Watch Now →
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2675 views
  • 2 likes
  • 3 in conversation