DATA Step, Macro, Functions and more

solve the query using nested macro

Reply
Frequent Contributor
Posts: 76

solve the query using nested macro

I have an data set like below student detail.


rollno studentname  subject        mark  division
1         A                     English          60      1
2        B                      English          80      1
4        D                     English          40      2
5         E                     English          80      1
6         F                     English          10      FAIL
1         A                     Science        70      1
2        B                      Science         30      3
3       C                       Science         50     2
4        D                     Science         40      2
5         E                     Science         40      2
6         F                     Science         50      2
1         A                     Math            70      1
2        B                      Math             80      1
3       C                       Math              90    1
4        D                     Math             50      2
5         E                     Math            50      2
1         A                     History         60      1
2        B                      History         80      1
3       C                       History         30     3
5         E                     History         80      1
6         F                     History         10      FAIL


I want to cteate a output table using above data as per student name .

for example if I call studant name A then it show detail  of student A like
rollno studentname  subject        mark  division
1         A                     English          60      1 
1         A                     History         60      1  
1         A                     Math            70      1
1         A                     Science        70      1     

if i call student name B the it show detail B like
rollno studentname  subject        mark  division
2        B                      English          80      1
2        B                      History         80      1 
2        B                      Math             80      1
2        B                      Science         30      3

As so on

there is no fix no of student .
Kindly help me how to write a program in macro so that if cal student name then come individual student data.
Regards,
Ashwini 

Super User
Super User
Posts: 6,498

Re: solve the query using nested macro

Macro's just generate SAS code. What SAS code do you want to generate?

For example you could make a subset of the data for a particular student.

data new ;

  set old;

  where studentname="A";

run;

To make this a macro, decide what you want as variables. 

For example if only the student name is variable your macro could look like this:

%macro subset(student);

data new;

  set old;

  where studentname="&student";

run;

%mend subset;

%subset(A);

Occasional Contributor
Posts: 14

solve the query using nested macro

data class;

input rollno studentname $  subject  $      mark  division $;

datalines;

1 A English 60 1

2 B English 80 1

4 D English 40 2

5 E English 80 1

6 F English 10 FAIL

1 A Science 70 1

2 B Science 30 3

3 C Science 50 2

4 D Science 40 2

5 E Science 40 2

6 F Science 50 2

1 A Math    70 1

2 B Math    80 1

3 C Math    90 1

4 D Math    50 2

5 E Math    50 2

1 A History 60 1

2 B History 80 1

3 C History 30 3

5 E History 80 1

6 F History 10 FAIL

;

Run;

%macro student(Name);

data student_&name;

SET class;

where upcase(studentname)=upcase("&name");

Run;

proc print data = student_&name;

rUN;

%mend;

%student(e);

Contributor
Posts: 60

solve the query using nested macro

take unique number of student in the input dataset,

proc sort data=stdudata out=tstdup nodupkey;

     by studentname;

run;

data _null_;

     set tstdup end=f;

     call symput("ST"||strip(put(_n_,3.)), strip(studentname));

     if f then call symput("nof",strip(put(_n_,3.)));

run;

%macro student;

%do h= 1 %to &nof;

   data student_&h;

      SET class;

          where upcase(studentname)=upcase("&&st&h");

          Run;

          proc print data = student_&h;

          rUN;

%end;

%mend student;

%student;

This program will print all the student, u dont want to call individual student by their name, it will take unique value from the input dataset and then print for all the students.

If you have any questions, let me know

Ask a Question
Discussion stats
  • 3 replies
  • 176 views
  • 0 likes
  • 4 in conversation