BookmarkSubscribeRSS Feed
Obsidian | Level 7

Hi I'm trying to run some basic statistics on variables that are numbered in sequence, however, not all the numbers exist in the sequence. The basic statistic tests exist in a separate macro (stddiff) that I'm calling from my do-looped macro and I only want it to call my statistics macro if the variable actually exists. I have variables from var001 - var899, however for example, var018 does not exist. This is currently what I have for my code:


%macro std_diff_loop (start,end); 

%do i=&start %to &end;

   %if %length(&start) eq 1 %then %do;
   %let i=%sysfunc(putN(&i,z3.));

   %if %length(&start) eq 2 %then %do;
   %let i=%sysfunc(putN(&i,z2.));


      %stddiff( inds = indicators, 
                groupvar = in_group, 
                numvars = , 
                charvars = var_n&i, 
                wtvar =,
               stdfmt = 8.4,
              outds = stddiff_result_var_&i ); 



When I run it this way, I get errors for the variables that don't exist since it can't run statistics on variables that don't exist.


Ideally what I'd like is for the macro to check my data set (indicators) before it calls the macro stddiff and only call the macro if the variable exists in the data set. Thanks!

Super User Tom
Super User

There are many such macros available.  Here is link to one:


Here is your macro updated to use it.

%macro std_diff_loop (start,end);
%local i ;
%do i=&start %to &end;
  %let i=%sysfunc(putn(&i,z%length(&end).));
  %if %varexist(indicators,var_n&i) %then %do;
    (inds = indicators
    ,groupvar = in_group
    ,numvars =
    ,charvars = var_n&i
    ,wtvar =
    ,stdfmt = 8.4
    ,outds = stddiff_result_var_&i

If you do use that version of the VAREXIST macro you can improve your macro's testing to make sure that it only tries to compute Std Dev on numeric variables.

  %if N=%varexist(indicators,var_n&i,type) %then %do;
Super User

Depending on you data structure you may be doing more work than needed. For instance, are you doing the same statistics on all the variables? Are they next to each other in the data set (column order)? Perhaps you can use the var1 - - varXXX shorthand to list adjacent variables such as below:


data example;
   set sashelp.class;
   rename height=var1;
   rename weight=var99;
   drop name sex age;

proc means data=example;
   var var1--var99;


Or if you variables have a unique base name then perhaps just the colon shorthand:  var:  would use all variables with the common name stem VAR.


Without seeing the actual code for you STD_DIFF macro it is difficult to see if other suggestions might be valid.


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
  • 2 replies
  • 3 in conversation