BookmarkSubscribeRSS Feed
endofline
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.));
   %end;

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

 

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


%end;
%mend;

 

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!

2 REPLIES 2
Tom
Super User Tom
Super User

There are many such macros available.  Here is link to one: https://github.com/sasutils/macros/blob/master/varexist.sas

 

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;
    %stddiff
    (inds = indicators
    ,groupvar = in_group
    ,numvars =
    ,charvars = var_n&i
    ,wtvar =
    ,stdfmt = 8.4
    ,outds = stddiff_result_var_&i
    )
  %end;
%end;
%mend;

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;
ballardw
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;
run;


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

 

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.

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
  • 3803 views
  • 0 likes
  • 3 in conversation