BookmarkSubscribeRSS Feed
mauri0623
Quartz | Level 8

In below code I am also interested in creating another macro variable called sum_weight from summing the weight variable. How do I build this macro variable?

 

%macro obsnvars(ds);
   %global dset nvars nobs;
   %let dset=&ds;

   /* Open data set passed as the macro parameter */
   %let dsid = %sysfunc(open(&dset));

   /* If the data set exists, get the number of observations */
   /* and variables and then close the data set */
   %if &dsid %then
   %do;
      %let nobs =%sysfunc(attrn(&dsid,nobs));
      %let nvars=%sysfunc(attrn(&dsid,nvars));
      %let rc = %sysfunc(close(&dsid));
    %end;

   /* Otherwise, write a message that the data set could not be opened */
   %else %put open for data set &dset failed - %sysfunc(sysmsg());
%mend obsnvars;

/* Execute the macro, passing TEST as the data set parameter */

%obsnvars(sashelp.class)


 
%put   &nvars variables
       &nobs observations
       Total weight = &sum_weight.

;

2 REPLIES 2
PaigeMiller
Diamond | Level 26

I would do the summing in PROC SUMMARY and not bother with all this macro stuff in the first place.

 

Note: If you need to determine if the data set exists, then the EXIST function in a simple macro will do that for you. If you need the results of the sum for later use, then you can assign it to a macro variable. Math (even something simple like summing) should be done in PROC MEANS or PROC SUMMARY (as the macro processor generally only does integer arithmetic, and even in that case, why write your own code to compute sums when SAS has done that for you?).

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 392 views
  • 4 likes
  • 3 in conversation