### How to explain HLM (response to an email)

Hello XXX, thanks for your email! I recently taught a premier for HLM, so I hope I can update my website with new material.

This time, I tried to say that HLM is simple, but equations make it difficult to learn (which is also the case for learning music instruments, like piano).

In the attached graph, I tried to compare my intuitive model (mental representation model) and a formal equation. When it is in my mind, coefficients are either random (red font) or fixed (blue). The red coefficients are adjusted for precision/reliability, while the blue ones are not (like OLS coefficients). I tried to show how the equation expresses the same idea, but I feel it quickly gets annoying.

### SAS Import & Export (for older Excel version)

PROC EXPORT DATA= sashelp.Verbmgr OUTFILE= "C:\temp\example.xls"
DBMS=EXCEL2000 REPLACE;
RUN;

PROC IMPORT OUT= exceldata
DATAFILE= "C:\temp\example.xls"
DBMS=EXCEL2000 REPLACE;
GETNAMES=YES;
RUN;

### SAS function: count the occurrance of a character

data test;
input transfer_matrix \$20.;
cards;
..0000000
11111111..
11000000001
1111111
111111111..
1111111000
0000.110
;
run;

data test;
set test;
count_1=countc(transfer_matrix,'1');
run;

proc print;
run;

Thanks, K.

### When merging datasets, be sure the same variable names are not used

Whe merging data A and B and if A and B has variables that happen to have the same variable name, one of them will be deleted. For example, if A has "GPA" meaning individual-level GPA and if B has GPA meaning school-level GPA, one of them will be deleted after the merge.

data A;
gpa=1.2;
run;

data B;
gpa=999;
run;

data both;
merge A B;
run;

proc print;
run;

RESULT:
Obs gpa
1 999

### Time function MDY( )

data x;
first=mdy(9,2,2012);
last=mdy(5,15,2013);
count=last-first;
run;
proc print;run;

### Factor analysis of Dichotomous Items

/*
http://support.sas.com/onlinedoc/913/getDoc/en/statug.hlp/factor_sect22.htm
http://support.sas.com/ctx/samples/index.jsp?sid=512
*/
/*place a SAS macro somewhere in your PC*/

%let dataname=amy;
%let name_of_doc=xxxxx\tetra_result.txt;

%include "xxxxxxxxxxxxx\sample00512_1_polychor.sas";

data raschdata;
set &dataname;
run;

data raschdata2;
set raschdata;
xxx=.;
keep
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

;
run;

%let abc=&syslast;
data _null_;set &abc end=end;
array test (*) _numeric_; array allmiss (3000) \$ (3000*'true');
length list \$ 5000; do i=1 to dim(test); if test(i) ne . then allmiss(i)='false';
end; if end=1 then do i= 1 to dim(test);
if allmiss(i) ='true' then list=trim(list)||' '||trim(vname(test(i)));
end; call symput('mlist',list);
run;
data &abc ; set &abc ; drop &mlist;run;
PROC SQL NOPRINT;
SELECT TRIM(NAME)
INTO :VARLIST SEPARATED BY ' '
FROM DICTIONARY.COLUMNS
WHERE LIBNAME EQ "WORK" AND MEMNAME EQ "RASCHDATA2"
ORDER BY VARNUM;
QUIT;

proc means data=raschdata2 ;
output out=meanresult;
var &varlist;
run;

data meanresult2;
set meanresult;
if _stat_="STD";
array kazu _numeric_ ;
do over kazu;
if kazu=0 then kazu=.;
end;
xxx=.;
run;
%let abc=&syslast;
data _null_;set &abc end=end;
array test (*) _numeric_; array allmiss (3000) \$ (3000*'true');
length list \$ 5000; do i=1 to dim(test); if test(i) ne . then allmiss(i)='false';
end; if end=1 then do i= 1 to dim(test);
if allmiss(i) ='true' then list=trim(list)||' '||trim(vname(test(i)));
end; call symput('mlist',list);
run;
data &abc ; set &abc ; drop &mlist _freq_ _stat_;run;

PROC SQL NOPRINT;
SELECT TRIM(NAME)
INTO :VARLIST SEPARATED BY ' '
FROM DICTIONARY.COLUMNS
WHERE LIBNAME EQ "WORK" AND MEMNAME EQ "MEANRESULT2"
ORDER BY VARNUM;
QUIT;

%polychor(data=raschdata,var=&varlist,maxiter=200,out=result);

/*just to get the name first*/
data result2;
retain _name_;
set result;
run;

PROC EXPORT DATA= result2 OUTFILE= "C:\temp\matrix.xls"
DBMS=EXCEL2000 REPLACE;
RUN;

option NOCENTER;
PROC PRINTTO PRINT="&name_of_doc" new;
RUN;
options formdlim=' ';

data one (type=corr);
set result2;
run;

proc factor data=one
method=prinit
n = 2
rotate = v
scree;
*where _type_="CORR";
var &varlist
;
run;

proc factor data=one
method=prin priors=smc
n = 2
rotate = v
scree;
var
&varlist
;
run;

PROC PRINTTO PRINT=PRINT;
run;
quit;

### Date variable: from String to Numeric with a date format

year=substr(DateOfService,1,4)+0;
D=substr(DateOfService,6,5);
x=indexc(D,"/")-1;
x2=indexc(D,"/")+1;
month=substr(D,1,x)+0;
day=substr(D,x2,2)+0;
s_date=MDY(month,day,year);

FORMAT date date9. ;

### PROC MEANS saving data that looks like the default output table

Only from SAS 9.3
http://support.sas.com/kb/46/427.html

ods output summary=outsum;

proc means data=sashelp.class stackodsoutput Mean Stddev Sum nway;
class age;
var height weight;
run;

ods output close;

Thanks, J.

### SAS Count function

data one;
input val \$9.;
cards;
102102111
108881111
;

data new;
set one;
temp=count(val,'1');
put temp=;
run;

RT

### Creating confidence intervals using datastep

X is a proportion (ranges from 0 to 1).  When alpha is set to 1.96, it creates 95% confidence intervals (two-tail tests).

N is the number of cases.

%let Alpha=1.96;

X_STDERR =sqrt(((X*(1-X))/N));
X_CI95_lower =X-(X_STDERR*&alpha);
X_CI95_upper =X+(X_STDERR*&alpha);

REFERENCE

http://davidmlane.com/hyperstat/B9168.html