BookmarkSubscribeRSS Feed
vivianclmnz
Calcite | Level 5

i need to get the values nearer to zero, for example in the below data, i need the fifth to eighth values which are the four (as required) nearest values to zero, plz help me with it, is there any specific function for it?

0.02253

0.01909

0.01545

0.01162

0.00758

0.00333

-0.00111

-0.00576

-0.01061

-0.01566

-0.02091

3 REPLIES 3
LinusH
Tourmaline | Level 20

ABS will help sort the data without regarding if it's negative or not.

Data never sleeps
UrvishShah
Fluorite | Level 6

Hi,

I am not aware about any function which will extract the values nearer to zero...However the following code might help you...

data have;

   input num;

   cards4;

0.02253

0.01909

0.01545

0.01162

0.00758

0.00333

-0.00111

-0.00576

-0.01061

-0.01566

-0.02091

0

;;;;

proc sort data = have out = want;

   by num;

run;

data _null_;

  set want;

  if num = 0 then do;

      call symput("obs1",_n_ - 2);

      call symput("obs2",_n_ + 2);

  end;

run;

data want;

  set want(firstobs = &obs1. obs = &obs2.);

run;

-Urvish

Vince28_Statcan
Quartz | Level 8

There are multiple alternatives.

The simplest mathematical approach is to use abs function to sort and just keep top 4. I'd use a proc sql order by to sort so as to avoid having to create a new variable with the absolute values

proc sql;

     create table have as

     select *

     from have

     order by abs(yourcolumnname);

quit;

data want;

     set have(obs=4);

run;

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
  • 3 replies
  • 1823 views
  • 9 likes
  • 4 in conversation