【问题标题】:SAS drive space checkSAS 驱动器空间检查
【发布时间】:2017-11-30 11:50:08
【问题描述】:

以下代码检查系统中所有可用驱动器的空间。我怎样才能让它只打印关于 G: 驱动器的信息,它也是一个网络驱动器?并以千兆字节而不是字节打印?谢谢:)

%let xmldata = %sysfunc(pathname(work))\wmic_output.xml;
%let xmlautomap  = %sysfunc(pathname(work))\wmic_output-automap.xml;
%let xmlmap =  %sysfunc(pathname(work))\wmic_output-map.xml;

filename wmic "&xmldata" encoding="utf-16";
filename wmicmap "&xmlmap";

filename gather pipe "wmic logicaldisk get name,size,freespace /format:rawxml > ""&xmldata""";

data _null_;
infile gather;
input;
put _infile_;
rc = sleep(.1,1);
run;

libname wmic xmlv2 automap=replace xmlmap=wmicmap;

proc copy in=wmic out=work;
run;

proc transpose data=work.property out=properties(drop=_name_) suffix=_text;
by instance_ordinal;
id property_name;
var value; 
run;

filename gather;
filename wmic;
filename wmicmap;
title1 "Available space in G: drive, if there is less than 1GB available,   please contact the team";
proc print data=work.property noobs;
run;
title; footnote;


title1 "There is less than 1GB available, please contact the BA team";
proc print data=work.property;
var PROPERTY_NAME value;
WHERE instance_ordinal=(4);
/*where property_name='FreeSpace';*/
run;
title; footnote;

【问题讨论】:

  • 你能编辑你的帖子吗?在每行的开头放置四个空格,以便正确呈现。您的一些 * 运算符变成了文本格式。
  • 刚刚做了,希望现在能用。
  • 拥有预览窗口的全部意义在于您可以在发布前检查降价是否有效。那没有。空格需要在行的开始,而不是在数字和代码之间。
  • 我明白了,但现在可以了吗?

标签: sas


【解决方案1】:

塔兹:

/format:rawxml 的优点是不需要写输入语句。缺点是您必须处理 XML 并对其进行重塑。

假设您删除了 /format:rawxml。 wmic 输出可以使用FIND 进行过滤——但是您将不得不处理不一致的输入数据。考虑 wmic 在我的系统上提供的未格式化输出。

> wmic logicaldisk get name,size,freespace
FreeSpace      Name  Size
356353708032   C:    536394588160
348834766848   D:    500104687616
               E:
68748967936    G:    128278560768
1173479329792  H:    3000579911680

E:是没有磁盘的蓝光驱动器。我想不出一个简单的单行输入语句可以无错误地处理 E: 行。这就是为什么我在回答您的其他问题时使用 rawxml 的原因。自动映射完成所有标准化输入,转置重塑数据。

但是,如果您过滤到您知道所有列中都包含数据的驱动器,您可以使用 remove /format 并将 FIND 添加到管道中。然后单个 DATA Step 可以读取结果、缩放字节并生成状态消息:

filename gspot pipe "wmic logicaldisk get name,size,freespace | find ""G:""";

%let status = G: *NOT FOUND*;
data _null_;
  infile gspot;
  attrib freespace length=8 name length=$2;
  input freespace name;
  freespaceGB = round(freespace / 1000**3,0.1);
  * robust if, in case pipe delivered more than expected;
  if name = "G:" then call symput ('status', "G: has " || cat(freespaceGB) || "GB free");
run;

%put NOTE: &=status;

如果您坚持使用 XML 输入法,您可以对转置的数据进行 DATA 单步处理以生成您的状态消息。在这个例子中,没有来自 wmic 库的 Proc COPY 工作——转置直接从 wmic 库中读取数据:

* ... libname prep ... *;

libname wmic xmlv2 automap=replace xmlmap=wmicmap;

proc transpose data=wmic.property out=work.properties(drop=_name_) suffix=_text;
by instance_ordinal;
id property_name;
var value; 
run;

您可以直接在转置输出数据集选项中进行过滤,例如out=work.properties(drop=_name_ where=(name_text="G:"))

生成您的状态消息

%let status = G: *NOT FOUND*;
data _null_;
  set work.properties;
  freespace = input (freespace_text, best12.);
  freespaceGB = round(freespace / 1000**3,0.1);
  if name_text = "G:" then call symput ('status', "G: has " || cat(freespaceGB) || "GB free");
run;

%put NOTE: &=status;

【讨论】:

  • 实际上,只有在G盘剩余1GB或更少时才以HTML打印! :)
【解决方案2】:

不知道为什么简单的输出将列的顺序与您要求的顺序不同。但是,如果它始终如一地做到这一点,那么它应该不难使用。要处理缺少的 FREESPACE 值,您可以尝试预读前两个字符。

data disks ;
  infile 'wmic logicaldisk get name,size,freespace' pipe firstobs=2 truncover ;
  length name $2 size freespace 8 ;
  input name $2. @1 @ ;
  if name = ' ' then input name ;
  else input freespace name size ;
  if name ne ' ';
  format size freespace comma32. ;
run;

所以对您的示例输出进行测试。

data disks ;
  infile cards firstobs=2 truncover ;
  length name $2 size freespace 8 ;
  input name $2. @1 @ ;
  if name = ' ' then input name ;
  else input freespace name size ;
  if name ne ' ';
  format size freespace comma32. ;
cards;
FreeSpace      Name  Size
356353708032   C:    536394588160
348834766848   D:    500104687616
               E:
68748967936    G:    128278560768
1173479329792  H:    3000579911680

;

产生这个结果。

【讨论】:

    【解决方案3】:

    wmic 在这里似乎有点矫枉过正——如果你想要的只是空闲空间,那么普通的旧 dir 怎么样?

    filename cmd pipe "dir G:\ | findstr /c:""bytes free""";
    
    data _null_;
      infile cmd;
      input;
      free_space_gb = input(scan(_infile_,3,' '), comma20.) * 2**-30;
      put "There is currently " free_space_gb 8.2 "GB of free space on the G drive";
      call symput('free_space_gb',free_space_gb); /*Create macro variable*/
    run;
    
    %macro print_alert_html;
      %if &free_space_gb < 1 %then %do;
        ods listing close;
        ods html file = "%sysfunc(pathname(work))\report.html";
          ods html text = "Alert: only &free_space_gb GB of space left on the G drive!";
        ods html close;
        ods listing;
      %end;
    %mend;
    

    然后您需要运行宏,如果它触发,您应该能够打开它生成的 html 报告:

    %print_alert_html;
    x explorer "%sysfunc(pathname(work))\report.html";
    

    【讨论】:

    • 实际上,只有在G盘剩余1GB或更少时才以HTML打印! :)
    • 这确实是一个单独的问题,但我在上面包含了一个基本示例。
    • marco print_alert_html 不起作用,您能否用 proc print data= 重新制定它?谢谢
    • 1.你运行宏了吗? 2. 条件是真的吗? 3.您是否尝试打开它生成的html?它在什么意义上“不起作用”?
    • 好吧,对不起,它可以工作,但只是输出应该使用 proc 打印而不是宏打印!谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多