Hello
I am trying to split a data set into multiple data sets.
In this example I get 5 data sets but the variable contains only 4 unique values.
What is the error in
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;
proc sort data=have;
by deptid;
run;
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;
%put &subj1;/*Maths*/
%put &subj2;/*Astronomy*/
%put &subj3;/*Physics*/
%put &subj4;/*Public Health*/
%put &total;/*4*/
%macro mmacro;
%do i=1 %to &total.;
data &&subj&i;
set have;
where deptdescription="&&subj&i";
run;
%end;
%mend mmacro;
%mmacro;
code?
"Public Health" is not a valid SAS name.
Hello
I want to ask a question about splitting a data set into multiple data sets by using macro.
In the following example there are 4 unique values to variable deptdescirption.
Why in the result there are 5 data sets?
What is the mistake in code here?
Why for value "Public Health" there are 2 data sets??
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;
proc sort data=have;
by deptid;
run;
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;
%put &subj1;/*Maths*/
%put &subj2;/*Astronomy*/
%put &subj3;/*Physics*/
%put &subj4;/*Public Health*/
%put &total;/*4*/
%macro mmacro;
%do i=1 %to &total.;
data &&subj&i;
set have;
where deptdescription="&&subj&i";
run;
%end;
%mend mmacro;
%mmacro;
those are all very good questions to ask.
Research and prior understanding of SAS tools you are requesting answers to would answer these basics questions like the space in the deptdescirption for deptid 4.
Know your data, have a basic understanding of the tools you are trying to use.
"Public Health" is not a valid SAS name.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.