Skip to content

Instantly share code, notes, and snippets.

View andyc1997's full-sized avatar
💭
Learning

AndyC_WC andyc1997

💭
Learning
View GitHub Profile
@andyc1997
andyc1997 / create_format.sas
Created June 21, 2025 14:57
Create user-defined format in SAS with the mapping defined in data step. Tested on SAS V9 Engine.
data fmtmapping;
infile datalines delimiter=',';
input Column1 Column2 $;
datalines;
1,City
2,Country
3,Town
;
run;
data fmtdata;
@andyc1997
andyc1997 / logging.sas
Created June 21, 2025 14:33
Wrap PROC PRINTTO in a macro function to trigger logging of a SAS program in deployment. Tested on SAS V9 Engine.
%let p_path = /home/xxxxx/yyyyy/;
%macro print_to_log(f_name=);
proc printto log="&p_path.&f_name..log";
run;
%mend print_to_log;
/* Comment the next line to disable logging */
%print_to_log(f_name=logfile);
@andyc1997
andyc1997 / show_user_macro_variables.sas
Created June 21, 2025 11:18
Display all macro variables created by the user if they are named in the way "PREFIX_". Tested on SAS V9 Engine.
title "User-generated Macro Variables";
proc sql;
select *
from sashelp.vmacro
where name like "PREFIX_%";
quit;
title;
@andyc1997
andyc1997 / dcreate_time.sas
Created June 21, 2025 10:51
Create an output directory using the current time as its name. Tested on SAS V9 engine.
%let p_path = /home/xxxxx/yyyyy/;
%let cur_date = %sysfunc(today(), yymmddn8.);
%let cur_hh = %sysfunc(hour(%sysfunc(time())));
%let cur_mm = %sysfunc(minute(%sysfunc(time())));
%let d_name = &cur_date._&cur_hh._&cur_mm.;
%put NOTE: folder name = &d_name;
data _null_;
rc = dcreate("&d_name", "&p_path");
@andyc1997
andyc1997 / proc_sql_create_macro_variables.sas
Last active June 21, 2025 10:12
Generate multiple macro variables with comma as the delimiter using proc sql into clause. Macro variables are initialised as empty strings. Tested on SAS V9 Engine.
%let macro_var1 = ' ';
%let macro_var2 = ' ';
proc sql noprint;
select distinct quote(strip(column1), "'"), quote(strip(column2), "'")
into :macro_var1 separated by ',', :macro_var2 separated by ','
from table;
quit;
%put macro_var1=&macro_var1;
%put macro_var2=&macro_var2;
@andyc1997
andyc1997 / file_backup.cmd
Last active May 3, 2025 17:09
A batch script using robocopy to backup directories and files to the folder that this script resides
@ECHO OFF
REM Change the title of CMD window to script name
TITLE %~nx0
SET year=%DATE:~10,4%
SET month=%DATE:~4,2%
SET day=%DATE:~7,2%
SET hour=%TIME:~0,2%
SET minute=%TIME:~3,2%
@andyc1997
andyc1997 / dpgan.py
Created December 20, 2023 13:18
Re-implementation of Differentially-Private GAN in pytorch with WGAN is our base model
# Generator (G)
class ganGenerator(nn.Module):
"""
Generator function
:param int p_latent: The dimension of the latent space
"""
def __init__(self, p_latent: int = p_f):
super(ganGenerator, self).__init__()
self.fc1 = nn.Linear(in_features=p_latent, out_features=c * 2 * 7 * 7)
@andyc1997
andyc1997 / vae_attack.py
Created December 20, 2023 12:34
Adversarial Attack on VAE based on Gondim-Ribeiro et al. (2018)
# VAE attack - input space
# objective function
li_delta = []
def obj_attack(delta, x, x_target, C):
h_temp = encoder(x + delta)[:, :10]
f1 = torch.sum((decoder(h_temp) - x_target) **2)
f2 = C*torch.sqrt(torch.sum(delta ** 2))
return f1 + f2
X <- sim.es(mu=2, p=5, k=3, alpha=0.3, max.time=50, init.yhat=1, init.m=1.5)
ggplot(data=X) + geom_point(aes(t, y, colour='y')) +
geom_line(aes(t, m, colour='m')) +
geom_line(aes(t, yhat, colour='yhat'), size=1.5) +
xlab('Time (t)') +
ylab('Demand forecast') +
theme(legend.position='bottom') +
scale_color_manual(name='Legend', values=c('y'='darkblue',
'm'='black',
'yhat'='red'))
sim.es <- function(mu, p, k, alpha, max.time, init.yhat, init.m) {
y <- demand.ts(mu, p, max.time)
e <- vector(mode="numeric", length=max.time)
yhat <- vector(mode="numeric", length=max.time)
m <- vector(mode="numeric", length=max.time)
R <- vector(mode="numeric", length=max.time)
yhat[1] <- init.yhat
m[1] <- init.m