BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

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?

 

1 ACCEPTED SOLUTION
3 REPLIES 3
Ronein
Onyx | Level 15

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;

 

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

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.

 

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1314 views
  • 1 like
  • 3 in conversation