BookmarkSubscribeRSS Feed
tomrvincent
Rhodochrosite | Level 12

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;
    
    /*%LET FIND_period = %INDEX(&lenfmt,%str(.)) ;
    %IF %upcase(&type)=N and %eval(&FIND_period) = 0 %THEN %LET lenfmt= &lenfmt.%str(.);*/
    
    %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;


2 REPLIES 2
saspert
Pyrite | Level 9

what is the issue here? do you have a an ERROR from the log?

tomrvincent
Rhodochrosite | Level 12

No 'issue' at all.  These are macros I wrote that I'm sharing.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 669 views
  • 2 likes
  • 2 in conversation