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-2024.png

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!

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.

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
  • 1 reply
  • 1153 views
  • 0 likes
  • 2 in conversation