Hi @Anv_SAS , I think this is what you wanted to achieve by the code you provided, however, you did not get what you desired. Here is my suggestion, please kindly let me know if it answer your question.
(1) you applied categories to the age and sex variables through the proc sort and merge steps, the code and output are as follows (in the output table, column n is age category and column m is sex category):
proc sort data=sashelp.class
out=class0 nodupkey;
by age;
run;
data class0;
set class0;
n=_n_;
run;
proc print data=class0;title "class0";run;
proc sort data=sashelp.class
out=class;
by age;
run;
proc print data=class;title "class";run;
data class2;
merge class(in=a)
class0(in=b keep=age n);
by age;
if a;
if sex="F" then m=1;
if sex="M" then m=2;
run;
proc print data=class2;title "class2";run;
(2) you were going to produce a macro variable which is the combination of the age and sex categories, and this macro variable resolves to respective students' names in the class as its' macro variable values. However, you did not choose the simple and efficient method to do this (you applied many techniques including macro, sql, nested do loop, which made the code difficult to debug). Here is my thinking and suggestion: you can simply use a single sql or data _null_ step to produce this macro variable.
proc sql;
select name,n,m
into :name1-,:n1-,:m1-
from class2;
quit;
%put _user_;
data _null_;
set class2;
call symputx(name,cat(n,m));
run;
%put _user_;
data _null_;
set class2;
call symputx(cat('age',n,m),name);
run;
%put _user_;
86 %put _user_;
GLOBAL AGE11 Joyce
GLOBAL AGE12 Thomas
GLOBAL AGE21 Louise
GLOBAL AGE22 Robert
GLOBAL AGE31 Barbara
GLOBAL AGE32 Jeffrey
GLOBAL AGE41 Judy
GLOBAL AGE42 Henry
GLOBAL AGE51 Mary
GLOBAL AGE52 William
GLOBAL AGE62 Philip
GLOBAL ALFRED 42
GLOBAL ALICE 31
GLOBAL BARBARA 31
GLOBAL CAROL 41
GLOBAL HENRY 42
GLOBAL JAMES 22
GLOBAL JANE 21
GLOBAL JANET 51
GLOBAL JEFFREY 32
GLOBAL JOHN 22
GLOBAL JOYCE 11
GLOBAL JUDY 41
GLOBAL LOUISE 21
GLOBAL M1 1
GLOBAL M10 2
GLOBAL M11 2
GLOBAL M12 1
GLOBAL M13 2
GLOBAL M14 1
GLOBAL M15 1
GLOBAL M16 1
GLOBAL M17 2
GLOBAL M18 2
GLOBAL M19 2
GLOBAL M2 2
GLOBAL M3 2
GLOBAL M4 1
GLOBAL M5 2
GLOBAL M6 1
GLOBAL M7 2
GLOBAL M8 1
GLOBAL M9 1
GLOBAL MARY 51
GLOBAL N1 1
GLOBAL N10 3
GLOBAL N11 4
GLOBAL N12 4
GLOBAL N13 4
GLOBAL N14 4
GLOBAL N15 5
GLOBAL N16 5
GLOBAL N17 5
GLOBAL N18 5
GLOBAL N19 6
GLOBAL N2 1
GLOBAL N3 2
GLOBAL N4 2
GLOBAL N5 2
GLOBAL N6 2
GLOBAL N7 2
GLOBAL N8 3
GLOBAL N9 3
GLOBAL NAME1 Joyce
GLOBAL NAME10 Jeffrey
GLOBAL NAME11 Alfred
GLOBAL NAME12 Carol
GLOBAL NAME13 Henry
GLOBAL NAME14 Judy
GLOBAL NAME15 Janet
GLOBAL NAME16 Mary
GLOBAL NAME17 Ronald
GLOBAL NAME18 William
GLOBAL NAME19 Philip
GLOBAL NAME2 Thomas
GLOBAL NAME3 James
GLOBAL NAME4 Jane
GLOBAL NAME5 John
GLOBAL NAME6 Louise
GLOBAL NAME7 Robert
GLOBAL NAME8 Alice
GLOBAL NAME9 Barbara
GLOBAL PHILIP 62
GLOBAL ROBERT 22
GLOBAL RONALD 52
(3) to conclude, the last data _null_ step might be the solution, which produces desired result and the macro variable &age11 was correctly resolved to a class student's name, Joyce, the code and results is as follows.
data _null_;
set class2;
call symputx(cat('age',n,m),name);
run;
%put &age11;
69 data _null_;
70 set class2;
71 call symputx(cat('age',n,m),name);
72 run;
NOTE: There were 19 observations read from the data set WORK.CLASS2.
73 %put &age11;
Joyce
... View more