Below is the script, In which I am trying to execute the "read sub" but unfortunately not able to get the expected result and giving the null value for "$sub".
Example: Below is the prompt massage which I am getting but the expected result should be in 2) example.
1)
Sub setting condition:check
Checking the macro []
2)
Sub setting condition:check
Checking the macro [check]
#!/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
Thank you in advance.
%sysexec spawns a process. read reads into a variable in the environment of that process. Once that process terminates (upon the return from the %sysexec), these changes are lost, and in the next spawned process from the next %sysexec the environment variable sub does not exist.
Making SAS batch programs interactive is counter-intuitive at best.
Hi Kurt,
Thank you for suggestion.
Below script is working for my problem.
#!/bin/sh
{
echo '%macro read;'
echo 'x "echo -n 'Sub setting condition:';
read sub;
echo Checking the macro [$sub];" '
echo '%mend;'
echo '%read;'
} > "/home/read.sas"
cd /home
sas /home/read.sas
I have kept all the command in only one X command so that echo and read command will work in a single execution.
If you want to feed user input from the commandline into a SAS program, do the read in the outermost shell script, and then retrieve the value in the SAS program with %sysget.
shell script example:
#!/usr/bin/bash # this script is written for AIX echo -n condition: read SUB export SUB sas $HOME/sascommunity/test.sas
SAS program test.sas:
%let inputval=%sysget(SUB);
x "/usr/bin/echo &inputval";
Now, I am using the SAS procedure within the SAS macro but read command is not working.
#!/bin/sh
x=$(pwd)
{ echo "option symbolgen mprint mlogic ;"
echo "libname sasdata '$x' access=readonly ;"
echo "%macro read;"
echo 'x "echo -n 'Enter Name:';
read name;
echo Datset name is [$name];" '
echo 'x cd $x;'
echo "libname sasdata '$x' access=readonly ;"
echo "proc freq data=sasdata.$name ;"
echo "tables _character_ / missing;"
echo "run;"
echo '%mend;'
echo '%read;'
} > $x/read.sas
cd $x
sas "$x/read.sas"
But here again $name is getting resolved.
read name;
is not part of the SAS program, but part of the shell script. And the shell script resolves $name for you and writes it into the SAS program:
echo "proc freq data=sasdata.$name ;"
What I do differently is that I do not write the program from the shell script, but instead have a static program text that is made dynamic through the use of the sysget() function.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.