Need a SAS macro program to avoid repetition -
i have dataset sas datastep logic needed populate columns missing, or derived exiting columns.
the dataset looks more below:
mpi v1 v2 v3......v9 v10 v11.....v50 001 1.324 002 c 0.876 003 f 11.9 004 r 5.7 005 b 3.3 . . . . . . n t 0.4
i developed programme below:
/*a*/ if v2 ('a') , 0 <= v11 <= 2 do; v13 = 1; v14 =20; end; if v2 in ('a') , 2 < v11 <= 3.1 do; v13 = 2; v14 =40; end; if v2 in ('a') , 3.1 < v11<= 5.3 do; v13 = 3; v14 =60; end; if v2 in ('a') , 5.3 < v11 <= 11.5 do; v13 = 4; v14 =80; end; if v2 in ('a') , v11 > 11.5 do; v13 = 5; v14 =100; end;
my request need write same programme populate v13 , v14 when v2 in c
, f
, t
, r
, etc; of different parameters bound in v11 (separate c
, e
, g
,...) while v13 , v14 remain same categories.
i utilize sas macro done avoid repetition of program. can help out on this?
the best way create dataset values of v2,v11,v13,v14, , merge on or otherwise combine dataset.
doing little more complicated when have range value, no means impossible.
let's have dataset, v2, v11min, v11max, v13, , v14.
data mergeon; input v2 $ v11min v11max v13 v14; datalines; 0 2 1 20 2 3.1 2 40 3.1 5.3 3 60 5.3 11.5 4 80 11.5 9999 5 100 c 0 4 1 20 c 4 8.1 2 40 c 8.1 9.6 3 60 c 9.6 13.5 4 80 c 13.5 9999 5 100 ;;;; run; info have; input mpi v2 $ v11 v13 v14; datalines; 1 2 0 0 2 4 0 0 3 c 1 0 0 4 c 7 0 0 5 c 9 0 0 6 22 0 0 7 10 0 0 ;;;; run; proc sql; create table want select h.mpi, h.v2, h.v11, coalesce(m.v13,h.v13) v13, coalesce(m.v14,h.v14) v14 have h left bring together mergeon m on h.v2=m.v2 , m.v11min < h.v11 <= m.v11max ; quit;
coalesce chooses first nonmissing value, meaning maintain h.v13 value when m.v13 missing (so, when merge fails find record in mergeon table).
if aren't comfortable sql, can utilize few other options; hash table easiest, though may able utilize update statement (not familiar myself).
sas sas-macro
No comments:
Post a Comment