Help using Base SAS procedures

Concatinate in to amacro variable

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

Concatinate in to amacro variable

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);


Accepted Solutions
Solution
‎04-16-2014 01:44 PM
Respected Advisor
Posts: 3,799

Re: Concatinate in to amacro variable

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


All Replies
Solution
‎04-16-2014 01:44 PM
Respected Advisor
Posts: 3,799

Re: Concatinate in to amacro variable

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;
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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