【问题标题】:How to use/execute "Read command of unix in sas macro"如何使用/执行“在 sas 宏中读取 unix 命令”
【发布时间】:2018-09-12 10:13:56
【问题描述】:

以下是我用来执行“读取子”的脚本,但不幸的是无法获得所需的结果。

#!/bin/sh

{
echo '%macro read;'

echo '%sysexec ( echo -n "Sub setting condition:");'
echo '%sysexec ( read sub) ;'
echo '%sysexec ( echo "Checking the macro [$sub]");'

echo '%mend;'

echo '%read;'
} > "/home/read.sas"
cd /home
sas /home/read.sas

以下是我对上述脚本的预期结果:

Checking the macro [<text after entering the sub setting condition:>]

提前感谢您的帮助。

【问题讨论】:

  • 你想做什么?如果您想与 SAS 交互,请使用 window%window 语句。或者可能学习 SCL 编程。
  • 嗨,汤姆,感谢您的回复。我正在尝试为 sas 程序编写一个 shell 脚本,但在上面的脚本中,“read”语句无法正常工作,它的“sub”值为空白。理想情况下,一旦我们输入一些字符串以提示“子设置条件:”,它应该具有“子”的值:
  • 如果read 命令在由正在运行的 SAS 程序启动的命令 shell 中运行,谁应该响应它?
  • 当我们运行 shell 脚本时,SAS 应该以批处理模式运行,%sysexec 函数将执行 unix 的读取命令。
  • 为什么?为什么不执行shell脚本中的read,使用数据生成SAS代码运行呢?这实际上可能有效,但您要尝试做的事情非常棘手。如果您确实需要从终端读取 SAS,请尝试使用 stdin 是您的 infile。

标签: macros sas


【解决方案1】:

您的输出可能保存在 sas 日志文件中。

参见下面的示例命令:指定要执行的文件以及保存日志文件的位置:

/sas/940/SASFoundation/9.4/sas /projects/program1.sas -log "/projects/program1.log" 

【讨论】:

    【解决方案2】:

    确保您正在执行的 .sas 文件是 SAS 程序,而不是仅包含宏功能的文件。

    【讨论】:

      【解决方案3】:

      你需要解释你想要做什么。您的程序可能会成功从控制台获取用户输入,但该值将存储在 SAS 代码无法访问的环境变量中。

      您可以通过将输入的值写入文件然后从文件中读取来检索输入的值。但是你需要确保read命令和引用环境变量read的命令在同一个子shell中运行。

      所以如果我将这个程序创建为read.sas

      data _null_;
        call system ( 'echo -n "Sub setting condition:";read sub ; echo "Sub is $sub" >read.txt ' );
      run;
      data _null_;
        infile 'read.txt';
        input;
        put _infile_;
      run;
      

      并使用sas read 运行它。那么 SAS 日志如下所示:

      1          data _null_;
      2            call system ( 'echo -n "Sub setting condition:";read sub ; echo "Su
      b is $sub" >read.txt ' );
      3          run;
      
      NOTE: DATA statement used (Total process time):
            real time           2.80 seconds
            cpu time            0.02 seconds
      
      
      4          data _null_;
      5            infile 'read.txt';
      6            input;
      7            put _infile_;
      8          run;
      
      NOTE: The infile 'read.txt' is:
            Filename=.../test/read.txt,
            Owner Name=xxxx,Group Name=xxx,
            Access Permission=-rw-rw-r--,
            Last Modified=05Apr2018:08:45:02,
            File Size (bytes)=16
      
      Sub is Hi there
      NOTE: 1 record was read from the infile 'read.txt'.
            The minimum record length was 15.
            The maximum record length was 15.
      NOTE: DATA statement used (Total process time):
            real time           0.00 seconds
            cpu time            0.00 seconds
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-19
        • 1970-01-01
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多