options ls=132 ps=53 nocenter;
ods pdf file="C:\projects\PROMIS-29\example\export29+2.pdf";
**************************************************************;
** read in sample data and make adjustments for AC scoring;
** export to prom29plus2.csv for processing;
**************************************************************;
DATA prom29plus2;
INPUT ID 1-2
PFA11 3
PFA21 4
PFA23 5
PFA53 6
EDANX01  7
EDANX40  8
EDANX41  9
EDANX53 10
EDDEP04 11
EDDEP06 12
EDDEP29 13
EDDEP41 14
HI7      15
AN3      16
FATEXP41 17
FATEXP40 18
SLEEP109 19
SLEEP116 20
SLEEP20  21
SLEEP44  22
SRPPER11 23
SRPPER18 24
SRPPER23 25
SRPPER46 26
PAININ9  27
PAININ22 28
PAININ31 29
PAININ34 30
PC6R  31
PC27R 32
GLOBAL07 33-34
;
CARDS;
0155551212111132222211555511114500
0255552333212154554432331311112401
0355551112111222222311353311114500
0455551132122122122122555511115501
055555335511115555115555351111  00
0655554435333244332222332421112404
075555333221222222 22244352111  03
0855552323123343433222434411114502
0955552232313133432211443311113402
1055551112123233333322213211113500
1155552222111112211111555511114500
125555121211111212224344442112  00
;
RUN;
************************;

libname save "C:\projects\PROMIS-29\example";

************************;
DATA save.prom29plus2; SET prom29plus2;
* rename items for use by Assessment Center;	
rename 
id       = pin
SRPPER11 = SRPPER11_CaPS
SRPPER18 = SRPPER18_CaPS
SRPPER23 = SRPPER23_CaPS
SRPPER46 = SRPPER46_CaPS;

* only one timepoint in this example: set up ASSMNT variable;
assmnt=1;
run;

DATA prom29plus2; SET save.prom29plus2;
	
PHYFUN4=MEAN(PFA11,PFA21,PFA23,PFA53)*4;
ANX4   =MEAN(EDANX01,EDANX40,EDANX41,EDANX53)*4;
DEP4   =MEAN(EDDEP04,EDDEP06,EDDEP29,EDDEP41)*4;
FAT4   =MEAN(HI7,AN3,FATEXP41,FATEXP40)*4;
SLPD4  =MEAN(SLEEP109,SLEEP116,SLEEP20,SLEEP44)*4;
SOC4   =MEAN(SRPPER11_CaPS,SRPPER18_CaPS,SRPPER23_CaPS,SRPPER46_CaPS)*4;
PAIN4  =MEAN(PAININ9,PAININ22,PAININ31,PAININ34)*4;
CF2    =MEAN(PC6R,PC27R)*2;

run;
************************************************************;
PROC SORT data=prom29plus2; BY pin; RUN;
***********************************************************;
TITLE "Overall means"; run;
proc means maxdec=0 data=prom29plus2; var
PHYFUN4 PFA11 PFA21 PFA23 PFA53
ANX4    EDANX01 EDANX40 EDANX41 EDANX53
DEP4    EDDEP04 EDDEP06 EDDEP29 EDDEP41
FAT4    HI7 AN3 FATEXP41 FATEXP40
SLPD4   SLEEP109 SLEEP116 SLEEP20 SLEEP44
SOC4    SRPPER11_CaPS SRPPER18_CaPS SRPPER23_CaPS SRPPER46_CaPS
PAIN4   PAININ9 PAININ22 PAININ31 PAININ34
CF2     PC6R PC27R
global07;
run;
***********************************************************;
TITLE "Look at correlation among scales"; run;
proc corr data=prom29plus2;
	var phyfun4 anx4 dep4 fat4 slpd4 soc4 pain4 cf2 global07;
run;
***********************************************************;

/** check scales **/
%macro alph(scale, items);
TITLE "&scale"; run;
proc corr alpha nomiss data=prom29plus2; 
	var &items;
run;	
%mend;

%alph(PHYFUN4 ,PFA11 PFA21 PFA23 PFA53);
%alph(ANX4    ,EDANX01 EDANX40 EDANX41 EDANX53);
%alph(DEP4    ,EDDEP04 EDDEP06 EDDEP29 EDDEP41);
%alph(FAT4    ,HI7 AN3 FATEXP41 FATEXP40);
%alph(SLPD4   ,SLEEP109 SLEEP116 SLEEP20 SLEEP44);
%alph(SOC4    ,SRPPER11_CaPS SRPPER18_CaPS SRPPER23_CaPS SRPPER46_CaPS);
%alph(PAIN4   ,PAININ9 PAININ22 PAININ31 PAININ34);
%alph(CF2     ,PC6R PC27R);

TITLE "Look at SLEEP alpha without inconsistent case (pin NE 5)"; run;
proc corr alpha nomiss data=prom29plus2; where pin ne 5;
	var SLEEP109 SLEEP116 SLEEP20 SLEEP44;
run; 

***********************************************************;

/* size limitation in AC */
/* not relevant for this case, but may need to break up your data into separate batches */
/* refer to AC manual: 
 https://www.assessmentcenter.net/ac_scoringservice/templates/UserManual.pdf) */
/*
data prom29plus2; set prom29plus2;
	if _N_ <= 4000;
run;
*/

***********************************************************;
/* convert items to character variables (i1-i31) to change SAS . to blanks for use in AC 
 (AC requires missing to be blank or value of 'SKIP' - refer to AC manual: 
 https://www.assessmentcenter.net/ac_scoringservice/templates/UserManual.pdf) 
*/

data prom29plus2; length i1-i30 $1. i31 $2.; set prom29plus2;
array invar 
      pfa11 pfa21 pfa23 pfa53
      edanx01 edanx40 edanx41 edanx53
      eddep04 eddep06 eddep29 eddep41
      hi7 an3 fatexp41 fatexp40
      sleep109 sleep116 sleep20 sleep44
      srpper11_caps srpper18_caps srpper23_caps srpper46_caps
      painin9 painin22 painin31 painin34
      pc6r pc27r;
array outvar $ i1-i30;

do over invar;
if invar > . then outvar=put(invar,$1.);
   else outvar=" ";
end;
   
if global07 > . then i31=put(global07,$2.);   
   else i31="  ";
run;
/*proc contents data=prom29plus2; run;*/

TITLE "Check a few recodes" ; run;
proc freq data=prom29plus2; 
	tables 
	pfa11*i1 
	pfa21*i2 
	pfa23*i3 
	pfa53*i4
     edanx01*i5 
     edanx40*i6 
     edanx41*i7 
     edanx53*i8
     eddep04*i9 
     eddep06*i10
     eddep29*i11
     eddep41*i12
     hi7*i13
     an3*i14
     fatexp41*i15
     fatexp40*i16
     sleep109*i17
     sleep116*i18
     sleep20*i19
     sleep44*i20
	srpper11_caps*i21 
	srpper18_caps*i22 
	srpper23_caps*i23
	srpper46_caps*i24 
	painin9*i25
	painin22*i26
	painin31*i27
	painin34*i28
	pc6r*i29
	pc27r*i30
	global07*i31
/list missing; 
run; 

/* output header and data to file prom29plus2.csv */
data _null_; file "prom29plus2.csv" dlm=","; set prom29plus2;
if _N_=1 then put @1 
"PIN,Assmnt,pfa11,pfa21,pfa23,pfa53,edanx01,edanx40,edanx41,edanx53,eddep04,eddep06,eddep29,eddep41,
hi7,an3,fatexp41,fatexp40,sleep109,sleep116,sleep20,sleep44,srpper11_caps,srpper18_caps,srpper23_caps,srpper46_caps,
painin9,painin22,painin31,painin34,pc6r,pc27r,global07";

put @1 PIN assmnt i1-i31;
run;

***********************************************************;

ods pdf close;
***********************************************************;