Wednesday, January 16, 2019

SAS: Eliminate duplicate records without variables list


SAS: Eliminate duplicate records without variables list


Trick: Use noduprecs on the proc sort statement, and _ALL_ in the by statement.


proc sort data=work.with_dupes out=work.without_dupes noduprecs;
by _ALL_;
run;







Wednesday, January 9, 2019

SAS: Test scope of macro variable (created in a data step in a macro)



TEST SCOPE OF MACRO VARIABLE



SOURCE CODE FOLLOWS:



* Test scope of macro variable ;



options nomprint nomlogic nosymbolgen;



data work.x;

input a $ b $ ;

datalines;

X J

X K

Y K

Y J

Z J

Z K

;

run;





%macro inner_macro(A=);



%local macvar;



data _NULL_;

set work.x ( where = (  a = "&A" )) ;

put a= b=;

if (b = "K") then do;

     call symputx("macvar", "Y", "L");

end;

else do;

     call symputx("macvar", "N", "L");

end;

test=symget("macvar");

put test=;

output;

stop; * end after first obs passing where condition ;

run;



%put >>--> After data step &=macvar;



%mend inner_macro;





%macro outer_macro;



%inner_macro(A=X);  * expect macvar=N ;

%inner_macro(A=Y);  * expect macvar=Y ;

%inner_macro(A=Z);  * expect macvar=N ;



%mend outer_macro;



%outer_macro;






LOG FOLLOWS:



NOTE: The data set WORK.X has 6 observations and 2 variables.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

     





A=X B=J

TEST=N

NOTE: There were 1 observations read from the data set WORK.X.

      WHERE a='X';

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

     



>>--> After data step MACVAR=N



A=Y B=K

TEST=Y

NOTE: There were 1 observations read from the data set WORK.X.

      WHERE a='Y';

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

     



>>--> After data step MACVAR=Y



A=Z B=J

TEST=N

NOTE: There were 1 observations read from the data set WORK.X.

      WHERE a='Z';

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

     



>>--> After data step MACVAR=N




Thursday, January 3, 2019

SAS: PROC FORMAT PICTURE example.



SAS: PROC FORMAT PICTURE example


proc format ;
 picture ExcelDate
  LOW - HIGH = '%0m/%0d/%Y %0H:%0M %p' ( DATATYPE = DATETIME )
  ;
run;


data _NULL_;
set work.some_date;
EFFECTIVE_DTTM_TEXT = put( EFFECTIVE_DTTM , ExcelDate. -L ) ;
put EFFECTIVE_DTTM_TEXT=;
run;