BookmarkSubscribeRSS Feed
manoj_pandey
Fluorite | Level 6

 

 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.

5 REPLIES 5
Kurt_Bremser
Super User

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

manoj_pandey
Fluorite | Level 6

 

 

 

 

 

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

Kurt_Bremser
Super User

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";
manoj_pandey
Fluorite | Level 6

 

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.

Kurt_Bremser
Super User
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1105 views
  • 0 likes
  • 2 in conversation