DATA Step, Macro, Functions and more

assign the closet value

Reply
Regular Contributor
Posts: 168

assign the closet value

 

 

Hi all,

 

I have veritical data . I need to create a variable which is close to the value. I am createing a reqvar variable.

 

cond:

reqvar is missing then dy1 is missing or ev<dy1 or ev-dySmiley Saddy1 dy2 dy3 dy4 dy5)>80

assing val: for whose time point is close to ev

data have;

input sub ev cnt dy1 dy2 dy3 dy4 dy5 val1 val2 val3 val4 val5;

cards;

100 255 5 25 55 78 90 110 .5 .9 1.5 6 8

101 90   3 40 83 87 . . .6 .8 1.7 . .

103 24   2 25 30 .   . . .9 .9 . . .

104 100 5 80 90 99 180 200 1.8 1.9 2 6 9

;

run;

 

data want;

sub ev cnt reqvar

100 255 5   .                         (it is missing because ev -dy >80)

101 90 3   1.7                       (assigned closet day ev=90 & dy3 =87 then assign value val3=1.7)

103 24 2   .                          (it is missing because ev <dy1)

104 100 5   2                       assigned closet day ev=100 & dy3 =99, dy4 is 180 duration b/w ev-dy4 >80 so assign value       

 

                                                val3=2)

 

Thanks

Sam

Contributor hbi
Contributor
Posts: 66

Re: assign the closet value

Hi,

 

Give this a try. This can be done in fewer data steps, but I kept it simple for illustrative purposes. Enjoy!

 

hbi

 

 

DATA have;
INPUT sub ev cnt dy1 dy2 dy3 dy4 dy5 val1 val2 val3 val4 val5;
cards;
100 255 5 25 55 78  90 110  .5  .9 1.5 6 8
101  90 3 40 83 87   .   .  .6  .8 1.7 . .
103  24 2 25 30  .   .   .  .9  .9   . . .
104 100 5 80 90 99 180 200 1.8 1.9   2 6 9
;
RUN;


/* fix up dataset; find minimums; filter out unwanted computed values */
DATA temp;
  SET have;
  ARRAY dy_all  dy1-dy5;
  ARRAY dy_diff diff1-diff5;
  ARRAY val_all val1-val5;

  i=0;
  DO OVER dy_all;
    i+1;
    IF (ev - dy_all > 80) THEN dy_diff[i] = .;
    ELSE IF (ev < dy_all) THEN dy_diff[i] = .;
    ELSE                       dy_diff[i] = ev - dy_all;
  END;

  min_diff = MIN(of dy_diff[*]);

  DO j=1 TO 5;
    IF (NMISS(min_diff, dy_diff[j]) = 0) THEN DO;
      IF min_diff = dy_diff[j] THEN target_var = j;
    END;
  END;

  IF NOT MISSING(target_var) THEN reqvar = val_all[target_var];
RUN;


DATA want;
  SET temp;
  DROP dy1       dy2         dy3    dy4    dy5    
       val1      val2        val3   val4   val5   
       diff1     diff2       diff3  diff4  diff5  
       min_diff  target_var  i      j;
RUN;
Regular Contributor
Posts: 168

Re: assign the closet value

Thank you for the reponse!!! for the sample data it is working fine. but on my real data i got this error. and in my example data i gave only 5 dy variable(dy1-dy5) but in my realy data it has more than 100. so icreate a macro variable and assing the value to array like this

 

array dy_all dy1-dy&obs.

 

ERROR: Mixing of implicit and explicit array subscripting is not allowed.

 

 

Thanks

Sam

ERROR: Mixing of implicit and explicit array subscripting is not allowed. - See more at: http://compgroups.net/comp.soft-sys.sas/array-index/133326#sthash.gy1yuUJi.dpuf
ERROR: Mixing of implicit and explicit array subscripting is not allowed - See more at: http://compgroups.net/comp.soft-sys.sas/array-index/133326#sthash.gy1yuUJi.dpuf
ERROR: Mixing of implicit and explicit array subscripting is not allowed - See more at: http://compgroups.net/comp.soft-sys.sas/array-index/133326#sthash.gy1yuUJi.dpuf
Contributor
Posts: 20

Re: assign the closet value


 

 

array dy_all dy1-dy&obs.

 

ERROR: Mixing of implicit and explicit array subscripting is not allowed.

 

ERROR: Mixing of implicit and explicit array subscripting is not allowed. - See more at: http://compgroups.net/comp.soft-sys.sas/array-index/133326#sthash.gy1yuUJi.dpuf
ERROR: Mixing of implicit and explicit array subscripting is not allowed - See more at: http://compgroups.net/comp.soft-sys.sas/array-index/133326#sthash.gy1yuUJi.dpuf
ERROR: Mixing of implicit and explicit array subscripting is not allowed - See more at: http://compgroups.net/comp.soft-sys.sas/array-index/133326#sthash.gy1yuUJi.dpuf

Your array statement doesn't have the number of elements. Does this work?

 

array dy_all {&obs} dy1-dy&obs;

Regular Contributor
Posts: 168

Re: assign the closet value

Hi ,

 

I tried that one too. But no luck

 

Thanks

Sam

Super User
Super User
Posts: 6,498

Re: assign the closet value

[ Edited ]

Your array definition is fine.  You do not need to tell SAS in two places how many elements are in your array. So these are all equivalent definitions.

array dy_all dy1-dy100 ;
array dy_all (100) dy1-dy100;

%let n=100;
array dy_all dy1-dy&n ;
array dy_all (&n) dy1-dy&n ;

The error message is talking about how the array is REFERENCED, not how it is defined. You can use the "implicit" array reference of the DO OVER statement if you always reference the array that way. Once you start using explicit references using an index value like DY_ALL(i) then you need to use it everywhere.  So you would need to replace:

do over dy_all ;
   dy_all = ....
end;

with

do i=1 to dim(dy_all);
   dy_all(i)= ...
end;

 

Ask a Question
Discussion stats
  • 5 replies
  • 267 views
  • 0 likes
  • 4 in conversation