BookmarkSubscribeRSS Feed
Ashwini
Calcite | Level 5

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 

3 REPLIES 3
Tom
Super User Tom
Super User

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);

Pallav
Calcite | Level 5

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);

Sudhakar_A
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 742 views
  • 0 likes
  • 4 in conversation