BookmarkSubscribeRSS Feed

add, drop and rename field macros

Started ‎11-03-2017 by
Modified ‎11-03-2017 by
Views 1,709

 

I wrote these macros to add, drop or rename fields.  Each checks to see if the field already exists.  For addfield, 4th parameter is length for character field or format for numeric field.

 

%macro AddField(ds, field, type, lenfmt);

    %local rc dsid result resx;

    %let dsid = %sysfunc(open(&ds));
    %let resx = %sysfunc(varnum(&dsid, &field));
    %let rc = %sysfunc(close(&dsid));
    %if %upcase(&type)=C %then %let spec=char(&lenfmt); %else %let spec=num;
   
    %if %upcase(&type)=N and %length(&lenfmt)>1 %then %let lenfmt=%str(format=)&lenfmt; %else %let lenfmt=;

    %if &resx > 0 %then %do;

        %let result = 1;
        %put NOTE: field &field already exists in &ds;

    %end;

    %else %do;

        %let result = 0;
        %put NOTE: field &field does not exist in &ds and will be added;

        proc sql;
            alter table &ds
                add &field &spec &lenfmt
        ;
        quit;    



    %end;
    
%mend AddField;



/* ********************************************************************** */

%macro DropField(ds, field);

    %local rc dsid result resx;

    %let dsid = %sysfunc(open(&ds));
    %let resx = %sysfunc(varnum(&dsid, &field));
    %let rc = %sysfunc(close(&dsid));

    %if &resx > 0 %then %do;

        %let result = 1;
        %put NOTE: field &field exists in &ds and will be dropped;
        proc sql;
            alter table &ds
                drop &field ;
        quit;
    %end;

    %else %do;

        %let result = 0;
        %put NOTE: field &field does not exist in &ds;

    %end;
    
%mend DropField;



/* ********************************************************************** */

%macro RenameField(ds, field, newname);

    %local rc dsid result resx;

    %let dsid = %sysfunc(open(&ds));
    %let resx = %sysfunc(varnum(&dsid, &field));
    %let rc = %sysfunc(close(&dsid));

    %if &resx > 0 %then %do;

        %let result = 1;
        %put NOTE: field &field exists in &ds and will be renamed to &newname ;

        data &ds;
            set &ds;
            /*&newname = &field ;
            drop &field ; */

            rename &field = &newname ;
        run;
        
    %end;

    %else %do;

        %let result = 0;
        %put NOTE: field &field does not exist in &ds;

    %end;
    
%mend RenameField;

Version history
Last update:
‎11-03-2017 08:18 AM
Updated by:
Contributors

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags