BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ginak
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20

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

4 REPLIES 4
LinusH
Tourmaline | Level 20

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
ginak
Quartz | Level 8

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!

Reeza
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 940 views
  • 4 likes
  • 4 in conversation