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

Hi

The catx is giving me error.If the variable is not in the dataset I need to concatinate all the missing variables and display a message "Missing required vaiables : show the names here).

I need to do it in a macro. If I can use scan and catx together then great.  If there is away to reduce the number of lines in the code would be great.

Any help is kindly appreciated! 

%Macro testm(inds=,  outds=);

   %let lst = %str(NAME,AGE,SEX,RACE,COUNTRY);

    %let dsid = %sysfunc(open(&inds));

    %let chk1 = %sysfunc(varnum(&dsid, NAME));

    %let chk2 = %sysfunc(varnum(&dsid,AGE));

    %let chk3 = %sysfunc(varnum(&dsid,SEX));

    %let chk4 = %sysfunc(varnum(&dsid,RACE));

    %let chk5 = %sysfunc(varnum(&dsid, COUNTRY));

        %let rc = %sysfunc(close(&dsid));

    %let lsto= ;

    %do i = 1 %to 5;

       %local var&i. ;

           %If &&chk&i = 0 %then

            %let var&i. =%scan(&lst., &i., %str(,));

          &lsto = %sysfunc(catx(%str( ), &lsto,  &&var&i.)));

        %end;

    %end;

   

%mend testm;

%testm(inds=C_TEST, outds=tt);

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

In macro language you don't need CAT functions.  Also don't use commas for variable list delimiters, it makes "everything" harder and SAS doesn't do it that way.

Here is what I think you want but in my opinion this is too much trouble.  I like to use PROC TRANSPOSE to check for this. See second example.

%Macro testm(inds=,lst=);
  
%local i w dsid lsto;
   %let dsid = %sysfunc(open(&inds));
   %let i = 1;
   %let w = %scan(&lst,&i,%str( ));
   %do %while(%superq(w) ne);
      %let rc = %sysfunc(varnum(&dsid,&w));
      %if &rc eq 0 %then %let lsto = &lsto &w;
      %let i = %eval(&i + 1);
      %let w = %scan(&lst,&i,%str( ));
      %end;
  
%let rc = %sysfunc(close(&dsid));
   %put NOTE: Variables do not exist "&lsto";
   %mend testm;

%
testm(inds=sashelp.class,lst=NAME AGE SEX RACE COUNTRY);

NOTE:
Variables do not exist "RACE COUNTRY"


%let lst=NAME AGE SEX RACE COUNTRY;
proc transpose data=sashelp.class(obs=0) out=names;
   var &lst;
   run;
15         %let lst=NAME AGE SEX RACE COUNTRY;
16         proc transpose data=sashelp.class(obs=0) out=names;
17            var &lst;
ERROR: Variable RACE not found.
ERROR: Variable COUNTRY not
found.
18            run;

View solution in original post

1 REPLY 1
data_null__
Jade | Level 19

In macro language you don't need CAT functions.  Also don't use commas for variable list delimiters, it makes "everything" harder and SAS doesn't do it that way.

Here is what I think you want but in my opinion this is too much trouble.  I like to use PROC TRANSPOSE to check for this. See second example.

%Macro testm(inds=,lst=);
  
%local i w dsid lsto;
   %let dsid = %sysfunc(open(&inds));
   %let i = 1;
   %let w = %scan(&lst,&i,%str( ));
   %do %while(%superq(w) ne);
      %let rc = %sysfunc(varnum(&dsid,&w));
      %if &rc eq 0 %then %let lsto = &lsto &w;
      %let i = %eval(&i + 1);
      %let w = %scan(&lst,&i,%str( ));
      %end;
  
%let rc = %sysfunc(close(&dsid));
   %put NOTE: Variables do not exist "&lsto";
   %mend testm;

%
testm(inds=sashelp.class,lst=NAME AGE SEX RACE COUNTRY);

NOTE:
Variables do not exist "RACE COUNTRY"


%let lst=NAME AGE SEX RACE COUNTRY;
proc transpose data=sashelp.class(obs=0) out=names;
   var &lst;
   run;
15         %let lst=NAME AGE SEX RACE COUNTRY;
16         proc transpose data=sashelp.class(obs=0) out=names;
17            var &lst;
ERROR: Variable RACE not found.
ERROR: Variable COUNTRY not
found.
18            run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1555 views
  • 0 likes
  • 2 in conversation