<<< Click here to go back
Consider the same example that we have discussed earlier.
Suppose we have a data set :Data Class;
infile datalines ;
Input Name $ Subject $ full_marks Marks_secured;
cards;
Rajat Maths 100 52
Vinod Maths 100 64
Rakesh Maths 100 57
Geeta Maths 100 58
Bharat Maths 100 86
Rajesh Maths 100 81
Monal Maths 100 67
Aarya Maths 100 88
Ritesh Maths 100 68
Neha Maths 100 88
Rajat English 150 53
Vinod English 150 69
Rakesh English 150 52
Geeta English 150 59
Bharat English 150 88
Rajesh English 150 59
Monal English 150 71
Aarya English 150 66
Ritesh English 150 68
Neha English 150 77
;
Run;
Now we need to break the data for each student individually. Also we don't want to write the names manually as we have done in the previous examples:
Links to previous examples : Creating student wise data using %Let
Now using a fusion of local and global variable, Let's make the world better!
Proc SQL;
select distinct name into : List separated by "|" from Class;
Quit;
Proc SQL;
select count (distinct name) into : N from Class;
Quit;
%Put &list.;
%Put &N.;
%Macro Break_the_data;
%Do i = 1 %to &N.;
%Let student = %Scan(&list.,&i., "|");
Data &student._data;
set class;
where name = "&student.";
Run;
%End;
%Mend;
%Break_the_data;
How the code worked :
Proc SQL;
select distinct name into : List separated by "|" from Class;
Quit;
Proc SQL;
select count (distinct name) into : N from Class;
Quit;
%Put &list.;
%Put &N.;
%Macro Break_the_data;
%Do i = 1 %to &N.;
%Let student = %Scan(&list.,&i., "|");
Data &student._data;
set class;
where name = "&student.";
Run;
%End;
%Mend;
%Break_the_data;
Enjoy reading our other articles and stay tuned with ...
Kindly do provide your feedback in the 'Comments' Section and share as much as possible.
Family of Macro related articles :
4. Conditional execution in Macro
5. Much more
No comments:
Post a Comment
Do provide us your feedback, it would help us serve your better.