This is an approach to put all observations of the same Deptid value into separate data sets named from the Datadescription variable.
data have;
infile datalines truncover;
input @1 deptid 1. deptdescription $ & 3-20;
datalines;
1 Maths
2 Astronomy
1 Maths
3 Physics
4 Public Health
3 Physics
;
run;
/* Sort by Deptid so first./last. logic can be used in the next step */
proc sort data=have;
by deptid;
run;
/* CALL SYMPUTX creates macro variables &Subj1, &Subj2, etc from the values of Deptdescription.*/
/* The total unique values of Deptdescription are stored in &Total */
data _null_;
set have;
by deptid;
if first.deptid then do;
i+1;
call symputx('subj'|| left(put(i,2.)),deptdescription);
end;
if last.deptid then call symputx('total',i);
run;
/* Within a macro %DO loop the data sets are created and those observations with the same */
/* Deptdescription values are read with a WHERE clause */
%macro test;
%do i=1 %to &total;
data &&subj&i;
set have;
where deptdescription="&&subj&i";
run;
%end;
%mend test;
/* invoke the macro */
%test
This is an approach to put all observations of the same Deptid value into separate data sets named from the Datadescription variable.
data have;
infile datalines truncover;
input @1 deptid 1. deptdescription $ & 3-20;
datalines;
1 Maths
2 Astronomy
1 Maths
3 Physics
4 Public Health
3 Physics
;
run;
/* Sort by Deptid so first./last. logic can be used in the next step */
proc sort data=have;
by deptid;
run;
/* CALL SYMPUTX creates macro variables &Subj1, &Subj2, etc from the values of Deptdescription.*/
/* The total unique values of Deptdescription are stored in &Total */
data _null_;
set have;
by deptid;
if first.deptid then do;
i+1;
call symputx('subj'|| left(put(i,2.)),deptdescription);
end;
if last.deptid then call symputx('total',i);
run;
/* Within a macro %DO loop the data sets are created and those observations with the same */
/* Deptdescription values are read with a WHERE clause */
%macro test;
%do i=1 %to &total;
data &&subj&i;
set have;
where deptdescription="&&subj&i";
run;
%end;
%mend test;
/* invoke the macro */
%test
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.