BookmarkSubscribeRSS Feed
Reeza
Super User

Here's a good primer on Macros. It's short read that should help clear things up. 

http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/

 

Also, review step one and step 4 in my instructions and compare it to your code, you made some mistakes but are close.

Bal23
Lapis Lazuli | Level 10

I am puzzled with step 4 and do not know how to modify my code.  Would you please let me know my mistakes?

Thanks.

%let sum=child adult senior over100;

 

 

%macro sum;

proc sort data=bp2∑
by patientid;
run;
PROC means data=bp2∑
var wvr_:;
run;

%mend;

%let data = %sum;


Reeza
Super User

@Bal23 Do you know how to run a macro? I don't understand the insistence of macro code - to me it sounds like homework then. 

 

Bal23
Lapis Lazuli | Level 10

I know the theory a little bit, but I do not know how to write

i usually modify very simple ones but get lost when there are new problems, that need me to be creative

would you please give advice

Reeza
Super User

I'm going back to my first point. 

  1. Create a single data set by stacking the 4 data sets together. This can be done via append or a data step. When stacking them together, create a variable that indicates which dataset an observation comes from. I demonstrated this with the INDSNAME option earlier in this thread.
  2. Run your freqs/means on the data set from #1. 

I highly recommend  this method because you get less errors and it's easier to implement for beginners. Learn the basics first.

In this structure you'll be able to easily compare freqs using a table statement. Let's assume that the variable that identifies your data set is called DATASET_NAME, then you can write your proc freq as follows. The variable values will be listed on the left and the data source across the top. I don't think your comparison can get any easier :).

 

Good Luck...I think I've chimed in enough on this thread. 

 

proc freq data=combined;
table my_var*dataset_name;
run;

 

 

Bal23
Lapis Lazuli | Level 10

Thanks. I have devloped code  previously, without macro; but I am interested in sas macro.

Thus I wrote my macro code, it works.

I am not very satified with the current code and think it can be improved. I hope to get advice from you how to make it more concise and be more efficient.

Then, go back to your post. I just do not know how to use INDSNAME option.

My problem is, I want to have sas code to compare the frequency of those variables "wvr_" and list it in that order by frequency.

Would you please give me better advice.

 

%let ageg= child;
%macro doit;
proc sort data=bp2&ageg;
by patientid;
run;
Title1 "2. body system, &ageg";
 PROC means DATA=bp2&ageg;
var wvr_:;
run;
%mend doit;
%let ageg= adult;
%doit
%let ageg= senior ;
%doit
%let ageg= over100;
%doit
run;
Reeza
Super User

 

Change your macro to take a parameter.

 

 

*this was step 4 in the instructions;
%macro doit(ageg);

***Rest of code***;

%mend;

%doit(child);
%doit(adult);
..
%doit(..);

 


Did you run the sample code I provided for INDSNAME? What doesn't make sense?

INDSNAME is short for INput DataSet NAME

That's what it does, identify what dataset the data came from.

Bal23
Lapis Lazuli | Level 10

Thank you. I read your posts several times. I have to piece your two posts together. To a certain degree, I am confused.

My sas got busy when I put those four datasets together. That slows down the process. Would you please put your two posts together so that I can see how it is?

 

Thanks.

 

Reeza
Super User

INDSNAME is not related to the macro. You can do this two ways.

 

  1. Stack datasets together and process as one. Most efficient and smart way to do it. This option uses the INDSNAME option.
  2. Use a macro and loop through each dataset and get multiple output tables. Inefficient way to do it. This does not use the INDSNAME option.

Pick one and go with it.

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 38 replies
  • 3232 views
  • 2 likes
  • 5 in conversation