Came across this nice macro. Good not only for work but for demoing the use of arrays and _numeric_.
%macro miss2zero;
array miss {*} _numeric_;
do i =1 to dim(miss);
if miss{i} = . then miss{i} = 0;
end;
%mend miss2zero;
Wednesday, December 23, 2015
Saturday, November 21, 2015
SAS: SQLOBS and creating macro variables within PROC SQL
I just came across the automatic macro variable SQLOBS...this example is fully self-contained...just copy it into SAS and run...
* ------------------------------------------------------------ ;
* DEMO USE OF SQLOBS WHEN CREATING MACRO VARIABLES FROM DATA
* ------------------------------------------------------------ ;
* CREATE TEST DATASET... ;
data work.grandkids;
input name $ gender $;
datalines;
Kamina F
Raelani F
Elliott M
Calliope F
Henni F
;
run;
* SHOW TEST DEMO DATASET... ;
proc print data=work.grandkids;
run;
* CREATE MACRO VARIABLES THE OLD WAY... ;
data _null_;
set work.grandkids end=eof;
call symput("name" || trim(left(_n_)), name);
call symput("gender" || trim(left(_n_)), gender);
if eof then call symput("howmany", trim(left(_n_)));
run;
%put howmany=&howmany;
* SELECT GRANDDAUGHTERS (ONLY)... ;
data work.granddaughters (keep=name);
attrib name length=$8;
do i = 1 to &howmany;
if (symget("gender" || trim(left(i))) = "F") then do;
name = symget("name" || trim(left(i)));
output;
end;
end;
run;
* SHOW GRANDDAUGHTERS... ;
proc print data=work.granddaughters;
run;
* CREATE MACRO VARIABLES THE NEW WAY... ;
proc sql noprint;
select name, gender
into :name1 - :name999, :gender1 - :gender999
from work.grandkids;
quit;
run;
* SHOW SQLOBS...THE WHOLE POINT OF THIS EXAMPLE... ;
%put sqlobs=&sqlobs;
* REASSIGN TO SQLOBS TO HOWMANY AND RUN OLD CODE AGAIN... ;
%let howmany = %eval(&sqlobs);
%put howmany=&howmany;
* SELECT GRANDDAUGHTERS (ONLY)... ;
data work.granddaughters (keep=name);
attrib name length=$8;
do i = 1 to &howmany;
if (symget("gender" || trim(left(i))) = "F") then do;
name = symget("name" || trim(left(i)));
output;
end;
end;
run;
* SHOW GRANDDAUGHTERS... ;
proc print data=work.granddaughters;
run;
Wednesday, November 11, 2015
Data Minng: Jaccard Similarity for bags
I was already familiar with the Jaccard Similarity index but not Jaccard Similarity for bags.
Source: http://infolab.stanford.edu/~ullman/mmds/ch3.pdf
"If ratings are 1-to-5-stars, put a movie in a customer’s set n times if they rated the movie n-stars. Then, use Jaccard similarity for bags when measuring the similarity of customers. The Jaccard similarity for bags B and C is defined by counting an element n times in the intersection if n is the minimum of the number of times the element appears in B and C. In the union, we count the element the sum of the number of times it appears in B and in C."
"Example 3.2 : The bag-similarity of bags {a, a, a, b} and {a, a, b, b, c} is 1/3. The intersection counts a twice and b once, so its size is 3. The size of the union of two bags is always the sum of the sizes of the two bags, or 9 in this case. Since the highest possible Jaccard similarity for bags is 1/2, the score of 1/3 indicates the two bags are quite similar, as should be apparent from an examination of their contents."
Saturday, October 24, 2015
SAS: Initialize random number seed
Initialize random number seed:
&let SEED = 1234;
data _null_;
call streaminit(&SEED);
run;
&let SEED = 1234;
data _null_;
call streaminit(&SEED);
run;
Saturday, October 17, 2015
R: Which package has this function?
Use the sos package in order to determine which package has a function you need.
library(sos)
findFn("partial residual plots")
Thursday, October 8, 2015
SAS: Drop variable if it exists
* =============================================================== ;
* M A C R O D R O P _ V A R _ I F _ E X I S T S
* =============================================================== ;
%macro drop_var_if_exists(SASFILE=,VAR=);
%* See also http://sasrunner.blogspot.com/2009/09/check-variable-exist-in-data-step-run.html;
data _null_;
dsid = open("&SASFILE");
call symput("var_exists", varnum(dsid, "&VAR"));
rc = close(dsid);
run;
%if (&var_exists eq 0) %then %do;
%put Variable &VAR does not exist in &SASFILE ;
%end;
%else %do;
proc sql noprint;
alter table &SASFILE drop &VAR;
quit;
run;
%put Variable &VAR was dropped from file &SASFILE ;
%end;
%mend drop_var_if_exists;
* =============================================================== ;
* T E S T M A C R O D R O P _ V A R _ I F _ E X I S T S
* =============================================================== ;
* Create test dataset;
data work.mactest;
set sashelp.class (obs=10);
run;
title "Before drops";
proc print data=work.mactest;
run;
title;
%drop_var_if_exists(SASFILE=work.mactest, VAR=name);
%drop_var_if_exists(SASFILE=work.mactest, VAR=xyz);
%drop_var_if_exists(SASFILE=work.mactest, VAR=age);
title "After drops";
proc print data=work.mactest;
run;
title;
* Done ;
Wednesday, October 7, 2015
SAS: Remove carriage return from string
I had a problem with a dataset with a single string field, created using datalines, and the line ended with carriage return + line feed ('0a0d'x), but code was copied to Linux where it was run, and Linux ends its lines with line feed only ('0a'x), so the field contained the carriage return. I needed to strip the carriage return from the field. I added this line within the data step that created the file.
my_field = TRANWRD(my_field,'0D'x,'');
my_field = TRANWRD(my_field,'0D'x,'');
Subscribe to:
Posts (Atom)