the files that I am appending is temp_&seed, seed=100
after running the following codes there are 100 files named: temp_1, temp_2,.....temp_100.
I have to tell you I am very new to SAS or any coding, I have just started learning SAS 2 weeks ago, so you need to be very clear for me to understand. For example what does the ":" after libref.sasdata: mean?
thanks
this is my codes, the last section is the part that generates the temp_ files.
thank you
%macro simulation;
%let num_students=100;
%let num_skills=3;
%let num_item=%eval(2**&num_skills-1);
%let num_total=%eval(2**&num_skills);
%let num_reps=2;
%let mu1=0;
%let mu2=-1;
%let mu3=-1;
%let mu4=-1;
%let mu5=-1;
%let mu6=-1;
%let mu7=-1;
%let mu8=0;
%let datafile=dat&seed.;
%let commandfile=CDM_&seed.;
%let outputfile=OUT_&seed.;
%do seed=1 %to &num_reps;
/* ADDEDED:to create C directory(IN SAS_CDM) TO SAVE MY Q-Matrix file*/
LIBNAME sas_cdm "C:\SAS_CDM ";
data SAS_CDM.Q_Matrix;
RUN;
data SAS_CDM.Q_Matrix;
set all_skills2;
drop lambda1_1 lambda1_2 lambda1_3 lambda0 i;
run;
data nu1;
array nu_num{&num_total} nu_num1-nu_num&num_total;
array nu{&num_total} nu1-nu&num_total;
%do i=1 %to &num_total;
nu_num{&i}=exp(&μ&i);
%end;
nu_denom=sum(of nu_num1-nu_num&num_total);
%do j=1 %to &num_total;
nu{&j}=nu_num{&j}/nu_denom;
%end;
drop nu_denom nu_num1-nu_num&num_total;
run;
proc transpose data=nu1 out=nu2; run;
data nu;
set nu2(rename=(col1=nu));
combination=_n_;
drop _name_;
run;
data all_skills;
array skills{&num_skills} skills1-skills&num_skills;
do j=1 to 2;
do i=1 to &num_skills;
if j=1 then skills{i}=1;
else skills{i}=0;
if skills{j}=. then skills{j}=0;
end;
output;
end;
drop i j;
run;
proc means data=all_skills completetypes;
class skills1-skills&num_skills;
output out=all_skills1;
run;
data all_skills2;
set all_skills1;
array lambda1{&num_skills} lambda1_1-lambda1_&num_skills;
array skills{&num_skills} skills1-skills&num_skills;
num=%eval(&num_skills);
if _type_=(2**num)-1 and sum(of skills1-skills&num_skills) ne 0;
/*Lambda Intercept*/
lambda0=-(1.5**sum(of skills1-skills&num_skills));
/*Lambda Main Effect*/
do i=1 to &num_skills;
lambda1{i}=skills{i}*2;
end;
drop num _type_ _freq_;
run;
data all_skills3;
set all_skills2;
question=_n_;
run;
data all_skills4;
set all_skills3;
do combinations=1 to &num_total;
output;
end;
run;
data combinations1;
array attributes{&num_skills} attributes1-attributes&num_skills;
array skills{&num_skills} skills1-skills&num_skills;
set all_skills1(keep=skills1-skills&num_skills _type_);
num=%eval(&num_skills);
if _type_=(2**num)-1;
do i=1 to &num_skills;
attributes{i}=skills{i};
end;
drop skills1-skills&num_skills i _type_ num;
run;
data combinations;
set combinations1;
combinations=_n_;
run;
/*ADDED: To create the file in SAS_CDM folder as well*/
data sas_cdm.combinations;
set combinations1;
combinations=_n_;
run;
/*ADDED: To create the file in SAS_CDM folder as well*/
data sas_cdm.Class;
set combinations1;
combinations=_n_;
run;
*/ADDED: TO chaning the format of combination to ASCII file*/
libname sas_cdm 'c:\CDM';
data _NULL_;
set sas_cdm.combinations;
file "C:\SAS_CDM\class..dat";
put attributes1 attributes2 attributes3 combinations;
run;
proc sort data=sas_cdm.combinations; by combinations; run;
proc sort data=all_skills4; by combinations; run;
data all_data;
merge all_skills4 sas_cdm.class; /*cdm*/
array lambda1{&num_skills} lambda1_1-lambda1_&num_skills;
array sum_lam{&num_skills} sum_lam1-sum_lam&num_skills;
array skills{&num_skills} skills1-skills&num_skills;
array attributes{&num_skills} attributes1-attributes&num_skills;
by combinations;
do i=1 to &num_skills;
sum_lam{i}=lambda1{i}* attributes{i}*skills{i};
end;
logit=lambda0+sum(of sum_lam1-sum_lam&num_skills);
pi_ic=exp(logit)/(1+exp(logit));
run;
data simulated_students;
array nu_skill{&num_total} nu1-nu&num_total;
array cum_skill{&num_total} cum1-cum&num_total;
set nu1;
/*Find Cumulative Distribution*/
cum_skill{1}=nu_skill{1};
do i=2 to &num_total;
cum_skill{i}=cum_skill{i-1}+nu_skill{i};
end;
/*Simulate Students*/
do student_num=1 to &num_students;
rand_uni=ranuni(&seed);
combinations=1;
do j=1 to &num_item;
if cum_skill{j}
end;
do question=1 to &num_item;
output;
end;
end;
run;
proc sort data=simulated_students; by combinations question; run;
proc sort data=all_data; by combinations question; run;
data simulated_data;
merge simulated_students(keep=student_num combinations question in=main) all_data(keep=question combinations attributes1-attributes&num_skills skills1-skills&num_skills pi_ic);
by combinations question;
if main;
x_ic=ranbin(&seed,1,pi_ic);
run;
/* ADDED: To change the format of Q-Matrix to ascii file*/
libname sas_cdm 'c:\sas_CDM';
data _NULL_;
set sas_cdm.Q_Matrix;
file "C:\sas_CDM\Q_Matrix..dat";
put skills1 skills2 skills3;
run;
/* ADDEDED THIS:to change the name to Class*/
data Class_membership;
set nu;
rename combination=Class;
RUN;
/* ADDED: To change the format of Class_membership to ascii file*/
libname sas_cdm 'c:\sas_CDM';
data _NULL_;
set Class_membership;
file "C:\sas_CDM\Class_membership..dat";
put nu class;
run;
/*continue coding*/
proc sort data=simulated_data; by student_num combinations;run;
proc transpose data=simulated_data(keep=student_num combinations question x_ic) out=sas_cdm.student&seed(drop=_name_) prefix=question;
id question;
by student_num combinations;
run;
*/ADDED: To change the format of SAS to ASCII file*/
/* ADDED: To Create all Data Files*/
libname sas_cdm 'c:\CDM';
data _NULL_;
set sas_cdm.student&seed;
file "C:\CDM\&datafile..dat";
put student_num Combinations question1 question2 question3 question4 question5 question6 question7;
run;
/*Create a command File for Mplus*/
data _null_;
file "c:\cdm\&commandfile..inp";
put "TITLE: CRUM with only main Effect";
put "DATA: FILE IS c:\CDM\&datafile..dat;"; **** modified ****;
put "VARIABLE: NAMES= STUDENT CLASS X1-X7;";
PUT "USEVARIABLE = x1-x7;";
PUT "CATEGORICAL = x1-x7;";
PUT "CLASSES = c(8);";
PUT "ANALYSIS:";
PUT "TYPE=MIXTURE; !estimates latent classes;";
PUT "STARTS=0; !turn off multiple random start feature (disabled anyway);";
put "MODEL:";
PUT '%OVERALL%';
PUT "[C#1] (m1); !latent variable mean for attribute pattern [0,0,0];";
PUT "[C#2] (m2); !latent variable mean for attribute pattern [0,0,1];";
PUT "[C#3] (m3); !latent variable mean for attribute pattern [0,1,0];";
PUT "[C#4] (m4); !latent variable mean for attribute pattern [0,1,1];";
PUT "[C#5] (m5); !latent variable mean for attribute pattern [1,0,0];";
PUT "[C#6] (m6); !latent variable mean for attribute pattern [1,0,1];";
PUT "[C#7] (m7); !latent variable mean for attribute pattern [1,1,0];";
put '%c#1% !for attribute pattern [0,0,0];';
put "[x1$1] (t1_1); !threshold for item 1 LCDM kernel 1";
put "[x2$1] (t2_1); !threshold for item 2 LCDM kernel 1";
put "[x3$1] (t3_1); !threshold for item 3 LCDM kernel 1";
put "[x4$1] (t4_1); !threshold for item 4 LCDM kernel 1";
put "[x5$1] (t5_1); !threshold for item 5 LCDM kernel 1";
put "[x6$1] (t6_1); !threshold for item 6 LCDM kernel 1";
put "[x7$1] (t7_1); !threshold for item 7 LCDM kernel 1";
put '%c#2% !for attribute pattern [0,0,1];';
put "[x1$1] (t1_1); !threshold for item 1 LCDM kernel 1";
put "[x2$1] (t2_1); !threshold for item 2 LCDM kernel 1";
put "[x3$1] (t3_2); !threshold for item 3 LCDM kernel 2";
put "[x4$1] (t4_1); !threshold for item 4 LCDM kernel 1";
put "[x5$1] (t5_2); !threshold for item 5 LCDM kernel 2";
put "[x6$1] (t6_2); !threshold for item 6 LCDM kernel 2";
put "[x7$1] (t7_2); !threshold for item 7 LCDM kernel 2";
put '%c#3% !for attribute pattern [0,1,0];';
put "[x1$1] (t1_1); !threshold for item 1 LCDM kernel 1";
put "[x2$1] (t2_2); !threshold for item 2 LCDM kernel 2";
put "[x3$1] (t3_1); !threshold for item 3 LCDM kernel 1";
put "[x4$1] (t4_2); !threshold for item 4 LCDM kernel 2";
put "[x5$1] (t5_1); !threshold for item 5 LCDM kernel 1";
put "[x6$1] (t6_3); !threshold for item 6 LCDM kernel 3";
put "[x7$1] (t7_3); !threshold for item 7 LCDM kernel 3";
put '%c#4% !for attribute pattern [0,1,1];';
put "[x1$1] (t1_1); !threshold for item 1 LCDM kernel 1";
put "[x2$1] (t2_2); !threshold for item 2 LCDM kernel 2";
put "[x3$1] (t3_2); !threshold for item 3 LCDM kernel 2";
put "[x4$1] (t4_2); !threshold for item 4 LCDM kernel 2";
put "[x5$1] (t5_2); !threshold for item 5 LCDM kernel 2";
put "[x6$1] (t6_4); !threshold for item 6 LCDM kernel 4";
put "[x7$1] (t7_4); !threshold for item 7 LCDM kernel 4";
put '%c#5% !for attribute pattern [1,0,0];';
put "[x1$1] (t1_2); !threshold for item 1 LCDM kernel 2";
put " [x2$1] (t2_1); !threshold for item 2 LCDM kernel 1";
put "[x3$1] (t3_1); !threshold for item 3 LCDM kernel 1";
put "[x4$1] (t4_3); !threshold for item 4 LCDM kernel 3";
put "[x5$1] (t5_3); !threshold for item 5 LCDM kernel 3";
put "[x6$1] (t6_1); !threshold for item 6 LCDM kernel 1";
put "[x7$1] (t7_5); !threshold for item 7 LCDM kernel 5";
put '%c#6% !for attribute pattern [1,0,1];';
put "[x1$1] (t1_2); !threshold for item 1 LCDM kernel 2";
put "[x2$1] (t2_1); !threshold for item 2 LCDM kernel 1";
put "[x3$1] (t3_2); !threshold for item 3 LCDM kernel 2";
put "[x4$1] (t4_3); !threshold for item 4 LCDM kernel 3";
put "[x5$1] (t5_4); !threshold for item 5 LCDM kernel 4";
put "[x6$1] (t6_2); !threshold for item 6 LCDM kernel 2";
put "[x7$1] (t7_6); !threshold for item 7 LCDM kernel 6";
put '%c#7% !for attribute pattern [1,1,0];';
put "[x1$1] (t1_2); !threshold for item 1 LCDM kernel 2";
put "[x2$1] (t2_2); !threshold for item 2 LCDM kernel 2";
put "[x3$1] (t3_1); !threshold for item 3 LCDM kernel 1";
put "[x4$1] (t4_4); !threshold for item 4 LCDM kernel 4";
put "[x5$1] (t5_3); !threshold for item 5 LCDM kernel 3";
put "[x6$1] (t6_3); !threshold for item 6 LCDM kernel 3";
put "[x7$1] (t7_7); !threshold for item 7 LCDM kernel 7";
put '%c#8% !for attribute pattern [1,1,1];';
put "[x1$1] (t1_2); !threshold for item 1 LCDM kernel 2";
put "[x2$1] (t2_2); !threshold for item 2 LCDM kernel 2";
put "[x3$1] (t3_2); !threshold for item 3 LCDM kernel 2";
put "[x4$1] (t4_4); !threshold for item 4 LCDM kernel 4";
put "[x5$1] (t5_4); !threshold for item 5 LCDM kernel 4";
put "[x6$1] (t6_4); !threshold for item 6 LCDM kernel 4";
put "[x7$1] (t7_8); !threshold for item 7 LCDM kernel 8";
Put "MODEL CONSTRAINT: !used to define LCDM parameters and constraints";
put "!NOTE: Mplus uses P(X=0) rather than P(X=1) so terms must be multiplied by -1";
put "!One attibute measured: 1 intercept; 1 main effect";
put "NEW(l1_0 l1_11); !define LCDM parameters present for item 1";
put "t1_1=-(l1_0); !set equal to intercept by LCDM kernel";
put "t1_2=-(l1_0+l1_11); !set equal to intercept plus main effect for attribute 1";
put "l1_11>0; !make sure main effect is positive (higher probability for mastering";
put "!ITEM 2:";
Put "!Q-matrix Entry [0 1 0]";
put "!One attibute measured: 1 intercept; 1 main effect";
put "NEW(l2_0 l2_12); !define LCDM parameters present for item 2";
put "t2_1=-(l2_0);";
put "t2_2=-(l2_0+l2_12);";
put "l2_12>0; !the order constraints necessary for the main effect";
put " !ITEM 3:";
put "!Q-matrix Entry [0 0 1];";
put "!One attibute measured: 1 intercept; 1 main effect";
put "NEW(l3_0 l3_13); !define LCDM parameters present for item 3";
put "t3_1=-(l3_0);";
put "t3_2=-(l3_0+l3_13);";
put "l3_13>0; !the order constraints necessary for the main effect";
put " !ITEM 4:";
put "!Q-matrix Entry [1 1 0]";
put "!two attibutes measured: 1 intercept; 2 main effects; 1 two-way interaction";
put "NEW(l4_0 l4_11 l4_12 ); !define LCDM parameters present for item 4";
put "t4_1=-(l4_0);";
put "t4_2=-(l4_0+l4_11);";
put "t4_3=-(l4_0+l4_12);";
put "t4_4=-(l4_0+l4_11+l4_12);";
put "l4_11>0; !the order constraints necessary for the main effects";
put "l4_12>0; ";
put "!ITEM 5:";
put "!Q-matrix Entry [1 0 1]";
put "!two attibutes measured: 1 intercept; 2 main effects; 1 two-way interaction";
put "NEW(l5_0 l5_11 l5_13); !define LCDM parameters present for item 5";
put "t5_1=-(l5_0);";
put "t5_2=-(l5_0+l5_11); ";
put "t5_3=-(l5_0+l5_13);";
put "t5_4=-(l5_0+l5_11+l5_13); ";
put "l5_11>0; !the order constraints necessary for the main effects";
put "l5_13>0;";
put "!ITEM 6:";
put "!Q-matrix Entry [0 1 1]";
put "!two attibutes measured: 1 intercept; 2 main effects; 1 two-way interaction ";
put "NEW(l6_0 l6_12 l6_13); !define LCDM parameters present for item 6";
put "t6_1=-(l6_0);";
put "t6_2=-(l6_0+l6_12);";
put "t6_3=-(l6_0+l6_13);";
put "t6_4=-(l6_0+l6_12+l6_13);";
put "l6_12>0;";
put "l6_13>0;";
put "!ITEM 7:";
put "!Q-matrix Entry [1 1 1]";
put "!two attibutes measured: 1 intercept; 3 main effects; 3 two-way interactions; 1 three-way";
put "NEW(l7_0 l7_11 l7_12 l7_13); !define LCDM parameters presen";
put "t7_1=-(l7_0);";
put "t7_2=-(l7_0+l7_13);";
put "t7_3=-(l7_0+l7_12);";
put "t7_4=-(l7_0+l7_12+l7_13);";
put "t7_5=-(l7_0+l7_11);";
put "t7_6=-(l7_0+l7_11+l7_13);";
put "t7_7=-(l7_0+l7_11+l7_12);";
put "t7_8=-(l7_0+l7_11+l7_12+l7_13);";
put "l7_11>0; !the order constraints necessa";
put "l7_12>0;";
put "l7_13>0;";
put "OUTPUT:";
put "TECH10; !request additional model fit statistics be reported";
put "SAVEDATA:";
put "FORMAT IS f10.5; !format for output file";
put "FILE IS c:\cdm\&outputfile..dat; !print attribute estimates for respondents in file list";
put "SAVE = CPROBABILITIES; !instruct Mplus to save posterior probabilities of class";
run;
* execute analysis with Mplus ;
x "cd C:\";
x "'C:\cdm\Mplus.exe' c:\cdm\&commandfile..inp";
LIBNAME cdm "C:\CDM ";
RUN;
* read the Information Criteria from the output file ;
data CDM.temp_&seed;
infile "c:\&commandfile..out" truncover;
input test $21. @;
if test="Information Criteria" then do;
input / / @40 num_fre_par
/ @40 akaike
/ @40 bayesian
/ @40 sam_siz_adj_bic ;
output;
stop;
end;
drop test;
run;
%end;
%mend;
%simulation;