BookmarkSubscribeRSS Feed
nbonda
Obsidian | Level 7

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

6 REPLIES 6
Astounding
PROC Star

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.

Patrick
Opal | Level 21

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.

 

😧 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>
nbonda
Obsidian | Level 7
Thank you for your input.
LinusH
Tourmaline | Level 20
@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
nbonda
Obsidian | Level 7

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

Patrick
Opal | Level 21

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 4968 views
  • 1 like
  • 4 in conversation