Skip to content

Instantly share code, notes, and snippets.

View rudvfaden's full-sized avatar

Rud Faden rudvfaden

View GitHub Profile
%macro embed(f);
%local p ref rc fid;
%let p = %sysfunc(findc(&f,/\:.));
%if &p %then %let rc = %sysfunc(filename(ref, &f));
%else %let ref = &f;
%let fid = %sysfunc(fopen(&ref));
%if &fid>0 %then
%do %while(%sysfunc(fread(&fid))=0);
%let rc = %sysfunc(fget(&fid, line, 32767));
&line
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run SAS",
"type": "shell",
"linux": {
"command": "<sas startup scriptZ>.sh",
@rudvfaden
rudvfaden / jbdw_create_folder.sas
Created October 28, 2021 07:30
recursivly creates folders in sas like mkdir -p in linux
%macro jbdw_create_folder(dname);
%* laver recurivet alle manglende mapper i en given sti;
%if %symexist(crdir)=0 %then %do;
%let crdir=;
*Erstatter \ med /;
%let dname = %sysfunc(translate(&dname,/,\));
*makes it indifferent wheter or not you add / to the end of the string;
%let dname=%sysfunc(tranwrd(%sysfunc(cats(&dname,/)),//,/));
%end;
@rudvfaden
rudvfaden / CprToBirthdaySql.sas
Last active June 3, 2021 09:09
Get birthday from danish cpr using case expression
%macro CprToBirthdaySql(cprnr);
case
when 0 <= input(substr(&cprnr,8,1),?1.) <= 9 and
0 <= input(substr(&cprnr,9,1),?1.) <= 9 then
case
when input(substr(&cprnr,7,1),1.) in (5,6,7,8) and
input(substr(&cprnr,5,2),2.) > 58 then
mdy(input(substr(&cprnr,3,2),2.),
input(substr(&cprnr,1,2),2.),
@rudvfaden
rudvfaden / AktivITidsinterval.sas
Created June 3, 2021 09:04
Tester om start og slutdato ligger i et tidsinterval
data have;
infile cards dsd dlm=',';
attrib id format=$1.;
attrib start informat=datetime20. format=datetime20.;
attrib slut informat=datetime20. format=datetime20.;
input id start slut;
cards;
1, 01-may-2020 08:00:00, 01-may-2020 12:00:00
1, 01-may-2020 09:00:00, 01-may-2020 10:00:00
1, 01-may-2020 13:00:00, 01-may-2020 22:00:00
@rudvfaden
rudvfaden / DimHelligdag.sas
Created May 28, 2021 06:49
Calculates Danish hollidays
%let _output=DimHelligdag;
proc datasets lib=DM_FL_HR nolist;
delete &_output;
run;
data &_output;
StartDato=intnx('year',today(),-6,'B');
Slutdato=intnx('year',today(),3,'E');
attrib &_OUTPUT1_col1_name length=&_OUTPUT1_col1_type&_OUTPUT1_col1_length;
do dato=StartDato to Slutdato;
@rudvfaden
rudvfaden / CprFunctions.sas
Created May 28, 2021 06:42
Validates cpr and calculates birthday and age
proc fcmp outlib=funk.function.funcs;
function isCpr(cpr $);
/* prxmatch (perl regular expresion) bruges til validering af cpr nummer.
*/
if prxmatch("/(?:(?:31(?:0[13578]|1[02])|(?:30|29)(?:0[13-9]|1[0-2])|(?:0[1-9]|1[0-9]|2[0-8])(?:0[1-9]|1[0-2]))[0-9]{3}|290200[4-9]|2902(?:(?!00)[02468][048]|[13579][26])[0-3])[0-9]{3}/",cpr) then retur=1;
else retur=0;
return(retur);
endsub;
@rudvfaden
rudvfaden / getEngine.sas
Created May 25, 2021 07:02
Retunrs sas engine from metadata
%macro getengine(libref);
%global ENGINE;
%let dsid=%sysfunc(open(sashelp.vlibnam(where=(libname="%upcase(&libref.)")),i));
%if (&dsid ^= 0) %then %do;
%let engnum=%sysfunc(varnum(&dsid,ENGINE));
%let rc=%sysfunc(fetch(&dsid));
%let engine=%sysfunc(getvarc(&dsid,&engnum));
&engine.
%let rc= %sysfunc(close(&dsid.));
%end;
@rudvfaden
rudvfaden / nobs.sas
Created May 25, 2021 06:56
Tries to count nobs by metadata, else default to recusion count.
%macro nobs(data=_last_);
%local dsid anobs whstmt counted rc;
%let DSID = %sysfunc(open(&DATA., IS));
%if &DSID = 0 %then %do;
%put %sysfunc(sysmsg());
%let counted = .;
%goto mexit;
%end;
%else %do;
@rudvfaden
rudvfaden / varExist.sas
Created May 25, 2021 06:54
Cheks if a SAS variable exists
***********************************************************************;
* Projekt : macropg
*
* Program name : VarExist.sas
*
* Path : /Y/nyfls/Statistikprogrammer/Macroprg/varExists.sas
*
* Author : Rud Faden
*
* Date created : 2018-05-14 12:50