DATA Step, Macro, Functions and more

How to use/execute “Read command of unix in sas macro”

Reply
Occasional Contributor
Posts: 6

How to use/execute “Read command of unix in sas macro”

 

 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.

Super User
Posts: 10,568

Re: How to use/execute “Read command of unix in sas macro”

Posted in reply to manoj_pandey

%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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 6

Re: How to use/execute “Read command of unix in sas macro”

Posted in reply to KurtBremser

 

 

 

 

 

#!/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. 

Super User
Posts: 10,568

Re: How to use/execute “Read command of unix in sas macro”

Posted in reply to manoj_pandey

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";
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 6

Re: How to use/execute “Read command of unix in sas macro”

[ Edited ]
Posted in reply to KurtBremser

 

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.

Super User
Posts: 10,568

Re: How to use/execute “Read command of unix in sas macro”

Posted in reply to manoj_pandey
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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 5 replies
  • 166 views
  • 0 likes
  • 2 in conversation