Help using Base SAS procedures

Proc IML to typical sas coding

Reply
Frequent Contributor
Posts: 126

Proc IML to typical sas coding

Hi all,

I found this interesting macro at a paper for imputing missing values using the nearest neighbor technique, my problem is that this macro contains a part done with proc iml , a procedure that i dont have in my sas base,

Ho would be possible to lets say translate it into normal code script?

Kind regards and thnx in advance, the macro is:

%MACRO NNI(INDATA=, /*INPUT DATA SET(INCLUDING LIBRARY NAME) */

MISSVAR=, /*INPUT SINGLE VARIABLE WITH MISSING DATA*/

RESPVAR=, /*RESPONSE VARIABLE*/

IDVAR=, /*SUBJECT VARIABLE*/

OUTDATA= /*OUTPUT DATA SET WITH COMPLETE DATA*/

);

DATA OBSY_MISSX;

SET &INDATA;

IF &MISSVAR=.;

KEEP &IDVAR &RESPVAR;

RUN;

PROC MEANS DATA=&INDATA N NMISS NOPRINT;

VAR &MISSVAR;

OUTPUT OUT=OBSN N=OBSX NMISS=MISSX;

RUN;

DATA _NULL_;

SET OBSN;

CALL SYMPUT('OBSN',OBSX);

CALL SYMPUT('MISSN',MISSX);

RUN;

DATA SIMDATA_NOMISS;

SET &INDATA;

IF &MISSVAR NE .;

RUN;

PROC IML;

USE SIMDATA_NOMISS;

READ ALL VAR {&MISSVAR} INTO XMAT;

READ ALL VAR {&RESPVAR} INTO YMAT;

6

USE OBSY_MISSX;

READ ALL VAR {&IDVAR &RESPVAR} INTO MISSMAT;

TXMAT=T(XMAT);

TYMAT=T(YMAT);

DISTANCE=J(&MISSN,&OBSN,.);

MIND=J(&MISSN,1,.);

MISSVAR=J(&MISSN,&OBSN,.);

IMP=J(&MISSN,1,.);

DO I = 1 TO &MISSN;

DO J = 1 TO &OBSN;

DISTANCE[I,J]=ABS(MISSMAT[I,2]-TYMAT[,J]);

END;

MIND[I,]=MIN(DISTANCE[I,]);

END;

DO I = 1 TO &MISSN;

DO J = 1 TO &OBSN;

IF DISTANCE[I,J]=MIND[I,] THEN MISSVAR[I,J]=TXMAT[,J];

END;

IMP[I,]=MISSVAR[I,:];

END;

CNAME={"&IDVAR" "&RESPVAR" "&MISSVAR"};

IMPX=MISSMAT||IMP;

CREATE NNI FROM IMPX[C=CNAME];

APPEND FROM IMPX;

QUIT;

DATA &OUTDATA;

MERGE &INDATA NNI;

BY &IDVAR;

RUN;

%MEND;

SAS Super FREQ
Posts: 3,752

Re: Proc IML to typical sas coding

Posted in reply to chemicalab

This is better asked in the DATA step forum.  You can use PROC DISTANCE to find the distance between obs.

Frequent Contributor
Posts: 126

Re: Proc IML to typical sas coding

Thank you for the clarification, will check that

Ask a Question
Discussion stats
  • 2 replies
  • 189 views
  • 0 likes
  • 2 in conversation