-1

I need to break down this SAS macro that adds suffixes to some number of variables into pseudocode, but there are some parts of it I don't fully understand.

%macro add_suffix(lib,dsn, suffix);
    options pageno=1 nodate;
    OPTIONS OBS= 1;
    DATA GRIDWORK.TMP;
    SET &lib..&dsn.;
    RUN;

    proc sql noprint;
    select nvar into :num_vars
        from dictionary.tables
            where libname="GRIDWORK" and
                memname="TMP";
    select distinct(name) into :var1-
        :var%TRIM(%LEFT(&num_vars))
    from dictionary.columns
        where libname="GRIDWORK" and
            memname="TMP";
    quit;

    run;

    OPTIONS OBS= MAX;
    proc datasets library=&LIB;
        modify &DSN;
        rename
            %do i=1 %to &num_vars;
                 &&var&i=&&var&i..&suffix
            %end;
        ;
    quit;

    run;

    proc datasets library=&LIB;
        modify &DSN;
        rename pers_gen_key&suffix = pers_gen_key;
    quit;

    run;

    proc sql;
         drop table gridwork.tmp;
    quit;
%mend add_suffix;

1) In this part of the code:

 DATA GRIDWORK.TMP;
 SET &lib..&dsn.;
 RUN;

How can you have setting a dataset equal to two values? Is it setting GRIDWORK.TMP to the concatenation of &lib and &dsn? What exactly do the multiple periods mean here?

2) I understand that this section is storing variables in an array:

proc sql noprint;
    select nvar into :num_vars
        from dictionary.tables
            where libname="GRIDWORK" and
                memname="TMP";
    select distinct(name) into :var1-
        :var%TRIM(%LEFT(&num_vars))
    from dictionary.columns
        where libname="GRIDWORK" and
            memname="TMP";
quit;

How exactly do dictionary.tables and dictionary.columns work, and how do they differ from eachother in this context? Here is the documentation, I read through it but am still having trouble understanding what exactly is going on in this section of the code.

3) Towards the end of the macro we have:

OPTIONS OBS= MAX;
proc datasets library=&LIB;
    modify &DSN;
    rename
        %do i=1 %to &num_vars;
            &&var&i=&&var&i..&suffix
        %end;
    ;
quit;
run;

Here is the documentation for the proc datasets procedure. It says it names the library that the procedure processes. Does this mean that &dsn is part of the &lib library? I guess I am unsure of how libraries work in SAS. Are they built in, or user-defined? Why are they necessary, couldn't we just modify &DSN on its own?

Danzo
  • 553
  • 3
  • 13
  • 26
  • This is three questions in one - please limit to one question at a time. This largely shows that you don't have a basic understanding of SAS, though; the answers to all of the above questions could be gained by spending a few days reading SAS documentation and/or taking a basic SAS class. – Joe Jul 10 '15 at 15:13
  • Also take a look into turning on `option mprint;` and for even more debugging info, you can turn on `option mlogic symbolgen macrogen;`. That may help you understand what is happening. – Robert Penridge Jul 10 '15 at 15:23
  • @Joe, what's the proper protocol here, should I not have answered the question, delete my answer now? – Reeza Jul 10 '15 at 15:49
  • @Reeza In my opinion you shouldn't answer a bad question at all. But, there's really no hard and fast rules here... :) Honestly probably 80-90% of [tag:SAS] questions aren't really appropriate, we tend to be more lenient than we should be in theory (if this were [tag:java] or [tag:c++] they'd all be closed in a minute). Way too many 'code for me' questions. – Joe Jul 10 '15 at 15:52
  • @Joe Sorry about this, I'll be sure to research more before asking in the future. – Danzo Jul 10 '15 at 16:40

1 Answers1

0
  1. SAS has two level references, library name and then data set name. The first macro variable points to the library and the second to the data set name. A period tells the macro processor where the macro variable ends and the second period is used to separate the libname from the data set name.

  2. Its not storing in arrays, its creating macro variables. The Dictionary tables are metadata about your tables. I would recommend actually looking at them. The difference between the tables is that TABLES has information on your dataset and COLUMNS has information on the variables in each table.

  3. A library is simply a directory/folder where SAS datasets are stored. This allows SAS to reference different directories to save files, and allows users to implement organizational systems on their data. &dsn is a data set in the &lib folder.

I highly recommend you look into the %put statement and place it in various parts of the code to see exactly what the code is doing.

Reeza
  • 20,510
  • 4
  • 21
  • 38