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

sas-innovate-2024.png

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.

 

Register 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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