BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi,
Is it possible to set a password for Excel book from within SAS (Base or EG)?
1 REPLY 1
data_null__
Jade | Level 19
I don't know of a SAS PROC or function, but you can write a program, that writes a program that calls a program that sets the PW. Here in one example.

[pre]
filename IN '.\book1.xls';
filename OUT '.\book1PW.xls';
%let PW = PWvalue;



data _null_;
length script filevar command workbook1 workbook2 $256 pw $16;
script = catx('\',pathname('WORK'),'PASSWORD.vbs');
filevar = script;
workbook1 = pathname('in');
workbook2 = pathname('out');
pw = symget('PW');

file dummy1 filevar=filevar;
put 'Const ' workbook1=$quote256.;
put 'Const ' workbook2=$quote256.;
put 'Const ' pw=$quote18.;
put 'Set objExcel = CreateObject("Excel.Application")';
put 'objExcel.Visible = FALSE';
put 'objExcel.DisplayAlerts = FALSE';
put 'Set objWorkbook = objExcel.Workbooks.Open(workbook1)';

put 'objWorkbook.SaveAs workbook2,,pw';
put 'objExcel.Quit';

/* close the script file by opening another file */
filevar = catx('\',pathname('WORK'),'DUMMY2.vbs');
file dummy1 filevar=filevar;

/* look at the script, not necessary but may be useful */
infile dummy3 filevar=script end=eof;
do _n_ = 1 by 1 until(eof);
input;
putlog 'NOTE: ' _n_ 3. +1 _infile_;
end;

/* call the script */
command = catx(' ','cscript',quote(strip(script)));
infile dummy4 pipe filevar=command end=eof;
do until(eof);
input ;
putlog _infile_;
end;
stop;
run;
[/pre]

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 1 reply
  • 947 views
  • 0 likes
  • 2 in conversation