DATA Step, Macro, Functions and more

How to pass value of macro varialbe to Linux variable

Reply
Contributor
Posts: 40

How to pass value of macro varialbe to Linux variable

Hi

 

I have a shell script, within that I execute SAS code. Now I would like to pass a value of macro variable to linux environment.

I am using Korn shell, running sas program in batch mode.

for instance:

proc sql;

select count(*)

into :tbl_cnt

from test_table;

quit;

 

I tried to use following code in SAS, to pass value fo Macrovariable to linux.

X 'export Lvalue=&tbl_cnt ';  and

X 'export Lvalue="&tbl_cnt" ';

 

This one does not work.?

how can solve this problem.

 

Thank you

Super User
Posts: 5,075

Re: How to pass value of macro varialbe to Linux variable

It would be nice if there were a %SYSPUT function, sort of the opposite of %SYSGET.  But if your illustrated code is the right syntax for the EXPORT command, you would need to get rid of the single quotes to allow your macro variable reference to resolve.  An easy way (not guaranteed but definitely worth a try) would be to switch from X to %SYSEXEC:

 

%SYSEXEC export LValue=&tbl_cnt;

 

If you need quotes around &TBL_CNT, use double quotes, not single quotes.

 

Good luck.

Respected Advisor
Posts: 3,887

Re: How to pass value of macro varialbe to Linux variable

[ Edited ]

Besides of the single quotes which won't allow the SAS macro variable to resolve I believe the main issue is:

- The KSH is the parent, the SAS program is the child.

- The EXPORT command sets a UNIX variable to "global"; but global in this context is not the same than in SAS. EXPORT makes variables set in the parent available to its children - but EXPORT doesn't make variables from the children available to its parents.

 

I'm not a UNIX expert so here just some options you could try and see if they work.

 

A: Define the UNIX variables in your KSH parent shell script as global using EXPORT. Then populate the variables within SAS. (I'm not sure though how far environment separation goes. The variables as such will exist in the child process with the value set by the parent. I'm not sure if a value altered by the child will be available to the parent though).

 

B: In the SAS child process write to a file, something like ~/myvars.ksh, which creates the UNIX variables with values as you need them. Then include this file in your parent script for execution after the call to the SAS process.

 

C: There might also be an option using a PIPE but I would have to do some own research to come up with more than just stating that this is something which is eventually possible.

 

D: I'm not sure if this is possible but if you could call the SAS process ".../sas.sh -..." as included script using the dot syntax instead of a separate process then the ksh script and the SAS process would share the environment. 

Syntax would be: 

.  <path>/sas.sh -sysin <sasprog.sas>
Contributor
Posts: 40

Re: How to pass value of macro varialbe to Linux variable

Thank you for your input.
Super User
Posts: 5,255

Re: How to pass value of macro varialbe to Linux variable

@nbonda, what do you want to achieve?
If none of options @Patrick suggests work for you, there might be a workaround that didn't think of.
Data never sleeps
Contributor
Posts: 40

Re: How to pass value of macro varialbe to Linux variable

I had to do in different way as export didn't work. I used call system('touch /user/file.new'). In ksh, instead of value of variable I am checking the existence of the file.new file.  thank you all

Respected Advisor
Posts: 3,887

Re: How to pass value of macro varialbe to Linux variable

@nbonda

I believe you've chosen the easiest approach to get what you need (which is a good thing). It's a variation of my option B.

Make sure that you delete the token file after you've checked for its existence as else in the next run...

 

Also: If the same user could execute your program multiple times in paralell you will need some timestamp and/or PID as part of your token file name.

Ask a Question
Discussion stats
  • 6 replies
  • 839 views
  • 0 likes
  • 4 in conversation