SAS目录:文件提取

huangapple go评论64阅读模式
英文:

SAS catalog: file extraction

问题

有没有一种方法可以将SAS目录的内容提取到纯文本文件中?

可以在基本的SAS中打开目录并手动复制内容,但这似乎非常繁琐。

英文:

Is there a way to extract the contents of a sas catalog into plain text files?

One could open the catalog in base sas and copy stuff manually but this seems extremly cumbersome.

答案1

得分: 1

您可以使用 catalog 文件名访问方法将目录内容提取到文件中。

  1. 运行 proc catalog 并创建一个输出数据集以识别目录条目。让我们创建一个示例数据集,该数据集将存储在 SAS 目录中,然后查看该条目:
data _null_;
   file 'work.mycat.data.catams' catalog;

   do i = 1 to 10;
       put i;
   end;
run;

proc catalog cat=sasmacr;
    contents out=contents;
run;
  1. 打开 contents 数据集。您将看到四个重要变量:libnamememnamenametype。 您可以使用点分隔的字符串访问每个目录的内容:libname.memname.name.type。 在本例中,我们要提取 work.mycat.data.catams

  2. 使用以下数据步骤代码将内容写入外部文件:

data _null_;
   infile 'work.mycat.data.catams' catalog;
   file '/location/contents.txt';
   input;
   put _INFILE_;
run;

检查 contents.txt,您应该会看到数字 1-10。

现在让我们将这个通用化,适用于多个目录。以下宏将读取目录中的所有条目,并将其内容输出到以 LIBNAME.MEMNAME.NAME.TYPE.txt 命名的动态生成的目录中。每个目录基于条目的 memname:

Location
        |
        |--- MEMNAME1
        |           |--- LIBNAME.MEMNAME1.NAME.TYPE1.txt
        |           |--- LIBNAME.MEMNAME1.NAME.TYPE2.txt
        |           |--- LIBNAME.MEMNAME1.NAME.TYPE3.txt
        |           ...
        |--- MEMNAME2
        |           |--- LIBNAME.MEMNAME2.NAME.TYPE1.txt
        |           |--- LIBNAME.MEMNAME2.NAME.TYPE2.txt
        |           |--- LIBNAME.MEMNAME2.NAME.TYPE3.txt
        ...         ...

在指定输出目录时,请确保在 Linux 上包括结尾的 /,在 Windows 上包括结尾的 \

%macro extract_catalog(cat=, outdir=);

    /* 获取操作系统的路径分隔符 */
    %if(&sysscp. = WIN) %then %let _SLASH_ = \;
        %else %let _SLASH_ = /;

    /* 如果目录不存在则创建目录 */
    %let dlcreatedir_orig = %sysfunc(getoption(dlcreatedir));
    options dlcreatedir;

    /* 获取所有不同的 memname */
    proc sql noprint;
        select distinct memname 
        into :memnames separated by '|'
        from contents
        ;
    quit;

    /* 遍历所有的 memname */
    %do i = 1 %to %sysfunc(countw(&memnames., |));
        %let memname = %sysfunc(scan(&memnames., &i., |));
        %let out = &outdir.&memname.;

        /* 如果目录不存在则创建目录 */
        libname __temp__ "&out.";

        /* 将每个条目的内容写入文件夹 */
        data _null_;
            set contents;
            where memname = "&memname.";

            cat_entry = catx('.', libname, memname, name, type);

            call execute(catt(
                "data _null_;
                    infile '", cat_entry, "' catalog;",
                "file '", &out._SLASH_, cat_entry, ".txt';
                     input;
                     put _INFILE_;
                 run;"
                )
            );
        run;

        libname __temp__ clear;
    %end;

    options &dlcreatedir_orig.;
%mend;

%extract_catalog(cat=mycat, outdir=/location/);

这将为您提取多个目录的内容并将其保存到指定的目录结构中。

英文:

You can extract catalog content to a file with the catalog filename access method.

  1. Run proc catalog and create an output dataset to identify the catalog entries. Let's create a sample dataset that will be stored in a SAS catalog and then view the entry:

Code:

data _null_;
   file 'work.mycat.data.catams' catalog;

   do i = 1 to 10;
       put i;
   end;
run;

proc catalog cat=sasmacr;
    contents out=contents;
run;
  1. Open the contents dataset. You'll see four important variables: libname, memname, name, and type. You can access the contents of each catalog with the dot-separated string: libname.memname.name.type. In this case, we're going to extract work.mycat.data.catams.

  2. Use the following data step code to write the contents to an external file:

Code:

data _null_;
   infile 'work.mycat.data.catams' catalog;
   file '/location/contents.txt';
   input;
   put _INFILE_;
run;

Check contents.txt and you should see the numbers 1-10.

Now let's generalize this for many catalogs. The following macro will read all the entries in a catalog and output their contents to a dynamically generated directory with the name LIBNAME.MEMNAME.NAME.TYPE.txt. Each directory is based off the memname of the entry:

 Location
        |
        |--- MEMNAME1
        |           |--- LIBNAME.MEMNAME1.NAME.TYPE1.txt
        |           |--- LIBNAME.MEMNAME1.NAME.TYPE2.txt
        |           |--- LIBNAME.MEMNAME1.NAME.TYPE3.txt
        |           ...
        |--- MEMNAME2
        |           |--- LIBNAME.MEMNAME2.NAME.TYPE1.txt
        |           |--- LIBNAME.MEMNAME2.NAME.TYPE2.txt
        |           |--- LIBNAME.MEMNAME2.NAME.TYPE3.txt
        ...         ...

When specifying the output directory, be sure to include an ending / if you're on Linux or \ if you're on Windows.

%macro extract_catalog(cat=, outdir=);

    /* Get OS slash */
    %if(&sysscp. = WIN) %then %let _SLASH_ = \;
        %else %let _SLASH_ = /;

    /* Create a directory if it does not exist */
    %let dlcreatedir_orig = %sysfunc(getoption(dlcreatedir));
    options dlcreatedir;

    /* Get all distinct memnames */
    proc sql noprint;
        select distinct memname 
        into :memnames separated by '|'
        from contents
        ;
    quit;

    /* Loop over all memnames */
    %do i = 1 %to %sysfunc(countw(&memnames., |));
        %let memname = %sysfunc(scan(&memnames., &i., |));
        %let out = &outdir.&memname.;

        /* Create a directory if it does not exist */
        libname __temp__ "&out.";

        /* Write content of each entry to a folder */
        data _null_;
            set contents;
            where memname = "&memname.";

            cat_entry = catx('.', libname, memname, name, type);

            call execute(catt(
                "data _null_;
                    infile '", cat_entry, "' catalog;",
                    "file '&out.&_SLASH_", cat_entry, ".txt';
                     input;
                     put _INFILE_;
                 run;"
                )
            );
        run;

        libname __temp__ clear;
    %end;

    options &dlcreatedir_orig.;
%mend;

%extract_catalog(cat=mycat, outdir=/location/);

答案2

得分: 0

你可以使用INFILEFILEFILEVAR功能来简化整个过程。

示例:

使用四级文件名,而不是创建嵌套的文件夹结构。

data _null_;
  set sashelp.vcatalg;
  where libname = 'SASHELP' and memname like 'AFTOOLS';

* where also objtype in (... list of reasonable textual types ...) ;

  entry4 = catx('.', libname, memname, objname, objtype);
  file4 = 'c:\temp\catdump\' || trim(entry4) || '.txt';

  infile entry catalog filevar=entry4 ;
    file dump  filevar=file4 lrecl=32000;

  input;
  put _infile_;
run;

注意:我已经保留了代码部分,未进行翻译。

英文:

You can simplify the whole mass using FILEVAR feature of INFILE and FILE.

Example:

Use four level filenames instead of creating a nested folder structure.

data _null_;
  set sashelp.vcatalg;
  where libname = 'SASHELP' and memname like 'AFTOOLS';

* where also objtype in (... list of reasonable textual types ...) ;

  entry4 = catx('.', libname, memname, objname, objtype);
  file4 = 'c:\temp\catdump\' || trim(entry4) || '.txt';

  infile entry catalog filevar=entry4 ;
    file dump  filevar=file4 lrecl=32000;

  input;
  put _infile_;
run;

huangapple
  • 本文由 发表于 2023年2月23日 20:51:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定