Returning Values from SAS to KSH

Reply
New Contributor
Posts: 2

Returning Values from SAS to KSH

Hi,

I have a question.

We have a code which compares the count from different sources and if it does not matches, it should send a return value to the KSH Script which calls the SAS Program.

This should be sent without Aborting the program.

The X command or CALL SYSTEM does not work as I hear they have disabled it.

Is there any other option available?

Super User
Posts: 5,260

Re: Returning Values from SAS to KSH

Why don't want to abort the job?

Shouldn't the process end at all, or do wish to end it with an RC=0?

There might by some ways around this, but not knowing more of your application, it's hard give some good advice. Maybe it's better you talk to "they" and convince them to open up for you system calls.

Data never sleeps
New Contributor
Posts: 2

Re: Returning Values from SAS to KSH

Hi LinusH,

Thanks for your reply.

We have now configured and the X command is working. This is what I'm trying to do.

Step 1: Call SAS Code from KSH

Step 2: Set up a variable inside SAS Program using X Command and Export statement ( see below). I have tried the below with quotes as well. But still not working.

data _NULL_;

X export aaa=99;

run;

Step 3: I have to get the value of aaa from KSH. I tried the below and it is not working.

Echo $aaa

Any help on this is appreciated.

Super Contributor
Posts: 644

Re: Returning Values from SAS to KSH

You might want to use call system instead of X if you are invoking from within a data step.

SAS(R) 9.2 Companion for UNIX Environments

The X command is executed unconditionally, the data step in your example is redundant.

What I believe is happening (assuming the code following the X command is valid) is that the X command creates a shell for the command.  The shell is temporary and not at the same "level" as the instance of KSH that launched the SAS program and hence the environment variable is not visible to the echo command. 

I'm not sure of any way you can push environmental values back into the original shell, except as a return code from an abend.

Richard

Super User
Posts: 5,260

Re: Returning Values from SAS to KSH

I have the same feeling as , so if you still don't want to abend (why not?), you probably need to use some not so neat work-around, such as creating a file with your value, and have your KSH script read that instead.

Data never sleeps
Super User
Super User
Posts: 6,502

Re: Returning Values from SAS to KSH

That will not work as the X command (sysexec, pipe, etc) is calling new shell to process your statements.  So you cannot write back into the shell that is calling SAS.

Why not just write the value to a file somewhere that the shell program can then read?  You could even pass the name of the file from the shell script into the SAS program using the -SET or -SYSPARM command line option.  You could even write the value as a shell script that could be sourced by the KSH program.

Or use -stdio option to route the output of the LOG or LST SAS outputs so that your shell can read them.

Valued Guide
Posts: 3,208

Re: Returning Values from SAS to KSH

The x-command, when and if disabled, is maintained by your local support staff.

They are occupied/convinced that people using Unix are dangerous species they are to be hold off on that.

Solving that is talking and convincing them you are doing serious work for the same company.

SAS(R) 9.3 Companion for UNIX Environments (options Xcmd) at config and the Workspace/sp-server option in the metadata (script-setings). 

There are many methods to reach Unix-commands with slightly different behavior.

- Executing OS-level commans can be more controlled using piping. It waits and get the output/messages. SAS(R) 9.4 Companion for UNIX Environments

- SAS(R) 9.4 Macro Language: Reference %sysexec command using the macro-language support with sysrc.

- The X statement (open code)

- Datastep system SAS(R) 9.4 Functions and CALL Routines: Reference

There are many fucntions in SAS unless you are having a special case, it is very likely your question can be solved wiht SAS without the need using a Unix command.

---->-- ja karman --<-----
Trusted Advisor
Posts: 1,300

Re: Returning Values from SAS to KSH

#!/bin/ksh

MYSASVAR=`/usr/local/SAS/SASFoundation/9.2/sas -stdio -noautoexec -nolog 2>/dev/null << _SASEOF_

options nodate nonumber nocenter formchar=' ';

title ' ';

proc sql;

select count(*) from sashelp.class;

quit;

_SASEOF_

`

echo $MYSASVAR

19

Frequent Contributor
Posts: 81

Re: Returning Values from SAS to KSH

I tried the below code and Put statement is not working and is showing blank:

MYSASVAR=`/SAS/sas94/SASFoundation/9.4/sas -stdio -noautoexec -nolog 2>/dev/null << _SASEOF_

options nodate nonumber nocenter formchar=' ';
%let a=a b c d;

%put &a.;

_SASEOF_
`
echo $MYSASVAR

Trusted Advisor
Posts: 1,300

Re: Returning Values from SAS to KSH

%put goes to the wrong destination, it goes to log, not print.  This is you problem.

You can redirect with proc printto or using a different print method, such as data step or ods text.

Super User
Super User
Posts: 6,502

Re: Returning Values from SAS to KSH

Following up on 's note that LOG and LST output go to different file handles when using -STDIO option.

Perhaps you can just put the value to the log and then grep the log to find the value you want?

Try something like this on the SAS side:

proc sql noprint;

select count(*) into :count from sashelp.class;

quit;

%put COUNT^&count ;

Then on the Unix side pass the LOG output from the SAS run into GREP and CUT to get the value of COUNT

.... | grep ^COUNT^ | cut -d^ -f1

Frequent Contributor
Posts: 81

Re: Returning Values from SAS to KSH

Hi

In your below code the value of MYSASVAR is not getting printed, rather it is the output of proc sql which is getting printed:

#!/bin/ksh

MYSASVAR=`/usr/local/SAS/SASFoundation/9.2/sas -stdio -noautoexec -nolog 2>/dev/null << _SASEOF_

options nodate nonumber nocenter formchar=' ';

title ' ';

proc sql;

select count(*) from sashelp.class;

quit;

_SASEOF_

`

echo $MYSASVAR

19

Can you please re-check it and let us know the idea by which we can get this value assigned to MYSASVAR variable.

THanks,

Daman

Trusted Advisor
Posts: 1,300

Re: Returning Values from SAS to KSH

That is exactly what it is meaning to do, put the output getting printed by PROC SQL into the KSH environment variable MYSASVAR.

Valued Guide
Posts: 3,208

Re: Returning Values from SAS to KSH

Several issues damanaulakh,

- You are having ubalanced apostrphes as of the formchar. It will recover within eGuide by the magic string.                                     

- You are probably wanting to coding a macrovar  MYSASVAR but the %let is not there.

-  is it open code or part of macro? When part of a macro what is the scope of the macrovar global/local and which of the local ones.      

-  The idea of redirecting data/code will not work that way. the Unix shell and SAS are segregated

- The return-code in a Unix script is a dedicated variable the $?.

   Be ware that sas is using a complicates nesting structure of all kind of scripts. This chain should give the executable sysrc value correct back to yours.

   A chain is as strong as the weakest part.

The ksh method for the returncode (just 2 lines of a long scritp) could be like:

   $SASROOT/sas $SASCONFG $SASOPTNS "$@"

    sasexit=$? ; export sasexit

---->-- ja karman --<-----
Valued Guide
Posts: 3,208

Re: Returning Values from SAS to KSH

Ah I see Fried Egg went to an bash script using imbedded sas-code (redirecting input) Learn Linux, 101: Streams, pipes, and redirects

I got lost with the original question and all technical proposals.  

"We have a code which compares the count from different sources and if it does not matches, it should send a return value to the KSH Script which calls the SAS Program.

This should be sent without Aborting the program."

Reading this question again, this looks to me a standard scheduler implementation reacting of a sysrc code of on a different event like the existence of some file

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 15 replies
  • 1426 views
  • 1 like
  • 8 in conversation