Hello,
I'm trying to write a macro which does a data step on several variables. I have a set of 4 variables for 20 different trauma types (lot of variables!) I'd like to sum over each of the 15 variables. The problem is, the number which indicates which trauma group its from is in the middle of the variable
so I have say, for each of the 20 groups:
gt1roy
gt1margie
gt1time
gt1tempo
gt2roy
gt2margie
gt2time
gt2tempo
.
.
.
gt20roy
gt20margie
gt20time
gt20tempo
I'd like to take a sum of gt1roy through gt1tempo (which has gt1=1 or 2 or 99). i'd like to sum over gt2roy through gt2tempo (which has gt2=1 or 2 or 99), ...., and finally gt20roy through gt20tempo (which has gt20=1 or 2 or 99). I'm interested in those cases with their gt variables = 2. And I want to do it when each group,
So I want somethign like
%macro sumz(n);
%do i = 1 %to &n.;
data new&i;
set olddataset;
where gt&i=2;
traumasum&i = sum (of gt&iroy gt&imargie gt&itime gt&itempo);
run;
%end;
%mend;
proc print data = new&i;
var idnumber gt&iroy gt&imargie gt&itime gt&itempo;
run;
%sumz(20);
But the problem is, sas doesn't recognize the index when it's in the middle of a variable. if all my variables ended with a number I could refer to variables like gtmargie&i gtroy&i etc and it would be fine. Help?Thanks!!
x
In this (an many many other) posts, it seems that "spreadsheet"-programming is deeply grounded among SAS programmers.
Without having tested, I'm pretty sure that a transposed data set is much easier to report on, you shouldn't even had to use a macro, no?
To anser your macro programming problem, if you want to resolve a macro variable in the middle of a text, you need to add dot as suffix, that will tell SAS that that's the end of the macro variable name.
In this (an many many other) posts, it seems that "spreadsheet"-programming is deeply grounded among SAS programmers.
Without having tested, I'm pretty sure that a transposed data set is much easier to report on, you shouldn't even had to use a macro, no?
To anser your macro programming problem, if you want to resolve a macro variable in the middle of a text, you need to add dot as suffix, that will tell SAS that that's the end of the macro variable name.
Ahh thank you!! Totally forgot about the dot. Also, I'm not sure how a transposed data set would work here? Each person (one record) has a value for all of those variables (maybe I'm misunderstanding your suggestion). But thanks everyone for your help!
I think Linus is indicating that it would be easier if you had multiple lines per patient, rather than your data structure of many variables.
Then you could just use BY group processing without macros
ie
PT_ID, Counter, roy, margie, time, temp
1, 1, 0, 1, 99, 2
1, 2, 0, 1, 99 2
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.