DATA Step, Macro, Functions and more

How Index on a number in a macro when it's in the middle of the variable

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

How Index on a number in a macro when it's in the middle of the variable

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


Accepted Solutions
Solution
‎02-10-2014 06:43 AM
Super User
Posts: 5,256

Re: How Index on a number in a macro when it's in the middle of the variable

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.

Data never sleeps

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: How Index on a number in a macro when it's in the middle of the variable

Solution
‎02-10-2014 06:43 AM
Super User
Posts: 5,256

Re: How Index on a number in a macro when it's in the middle of the variable

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.

Data never sleeps
Contributor
Posts: 67

Re: How Index on a number in a macro when it's in the middle of the variable

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!

Super User
Posts: 17,819

Re: How Index on a number in a macro when it's in the middle of the variable

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 Smiley Happy

ie

PT_ID, Counter, roy, margie, time, temp

1, 1, 0, 1, 99, 2

1, 2, 0, 1, 99 2

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 262 views
  • 4 likes
  • 4 in conversation