Sunday, January 19, 2025

Docker saspy

To keep my work environment clean, I always use docker to containize my faverite tool. I don't worry about the space as my work laptop has no any video. :) In my previous post, I have created script run_saspy.py to run SAS program locally. Below is my docker file to containize the script.
# Dockerfile
# Cmmand: docker build -t saspy .

FROM python

RUN apt update && apt install -y python3-pip && apt install -y default-jre
RUN pip install --upgrade pip
RUN pip install pandas
RUN pip install saspy

COPY sascfg_personal.py /usr/local/lib/python3.13/site-packages/saspy/sascfg_personal.py
COPY authinfo /root/.authinfo
COPY run_saspy.py /usr/local/bin/run_saspy.py

ENTRYPOINT ["run_saspy.py"]
When the docker image is built, I set alias run_saspy and it works perfectly.
$ alias run_saspy='docker run --rm -it -v ${PWD}:${PWD} -w "${PWD}" saspy -i '
$ run_saspy hello.sas
********************************************
Playpen: playpen
Using SAS Config named: oda
SAS Connection established. Subprocess id is 14

NOTE: hello.sas uploaded to ~/playpen/hello.sas successfully!
NOTE: ~/playpen/hello.20250120T003749.log downloaded to hello.20250120T003749.log successfully!
NOTE: Playpen killed successfully!
SAS Connection terminated. Subprocess id was 14
********************************************
********************************************
***        JOB EXECUTION STATUS          ***
********************************************
INFO: Finished successfully!
********************************************

Monday, November 11, 2024

saspy + SAS® OnDemand for Academics = as if SAS is right there beside you

Lately, I’ve had some downtime and I took the opportunity to finish a tool. This tool uploads code and data to SAS® OnDemand for Academics, executes them as if running locally, and then automatically diagnoses certain errors and returns the results once completed.
usage: run_saspy.py [-h] [-i INPUT_PGM] [-o OUTPUT_DIR [OUTPUT_DIR ...]] [-d DATA_UPLOAD [DATA_UPLOAD ...]] [-p PLAYPEN] [-k KILL]
                    [-c CFGNAME]

It executes SAS code using saspy. ONLY ONE LEVEL FOLDER SUPPORTED!

options:
  -h, --help            show this help message and exit
  -i INPUT_PGM, --input_pgm INPUT_PGM
                        (Required) Name of the SAS file to be executed.
  -o OUTPUT_DIR [OUTPUT_DIR ...], --output_dir OUTPUT_DIR [OUTPUT_DIR ...]
                        Remote directories in the current working folder, which will be created in ~// to save outputs.
  -d DATA_UPLOAD [DATA_UPLOAD ...], --data_upload DATA_UPLOAD [DATA_UPLOAD ...]
                        Local files or directories in the current working folder, which will be uploaded to ~//. NO ABSOLUTE
                        PATH ALLOWED!
  -p PLAYPEN, --playpen PLAYPEN
                        Name of playpen. Default is playpen.
  -k KILL, --kill KILL  (Y/N) Kill the playpen after completion.
  -c CFGNAME, --cfgname CFGNAME
                        Name of the Configuration Definition to use for the SASsession. If not specified then just saspy.SASsession()
                        is executed.

In the test below, I uploaded pgm and data, executed them and downloaded them locally. When all tests completed, it will check log and free up the space by cleaning up all playpen data. In this way, I will not worry about the space any more.

Thursday, September 22, 2022

Download SAS work dataset in Viya 4

In Viya 4 SAS Studio, we can not download directly from SAS libraries but export text file (e.g. comma-delimited, tab-delimited). As you can see, we can upload and download files from Contents. Therefore, we can leverage Viya Files service to download SAS datasets. Below is the sample code.
data result;
	set sashelp.class;
run;

filename out filesrvc folderpath='/Users/xxxxx/My Folder' 
	filename='result.sas7bdat' recfm=n lrecl=32767;
filename in "%sysfunc(pathname(work))/a.sas7bdat" recfm=n lrecl=32767;

data _null_;
    rc = fcopy('in', 'out');
    put rc=;
    length msg $1000;
    msg = sysmsg();
    put msg=;
run;

Wednesday, July 13, 2022

Split long string: VARCHAR + data _null_

As we know, the maximum length for character variable is 32767. To handle the string which is longer than 32767, we can split it, store in dataset and recomine it. Below is the sample code:
filename source "file_with_long_string";
filename target "file_with_new_long_string";

data source;
    infile source recfm=f lrecl=32000 pad;
    input text $char32000.;
run;

data target;
    length x $ 32767;
    set source;
    x = prxchange('s/old/new/', -1, text);
run;

data _null_;
    length y varchar(5000000);

    do until(eof);
        set target end=eof;
        y = cats(y, x);
    end;

    file target lrecl = 5000000;
    put y;
run;

Tuesday, June 7, 2022

Hash way to split SAS Table

In traditional SAS way, it will leverage SAS macro to split the SAS dataset. If you are bored with that, you may like the hash way below. Enjoy!
proc sort data=sashelp.class out=class;
    by age;
run;

data _null_ ;
    declare hash h (multidata:"Y", 
    				ordered: "a", 
    				dataset:"class(obs=0)") ;
    h.definekey('name') ;
    h.definedata(all:'Y');
    h.definedone() ;

    do i = 1 by 1 until (last.age);
        set class;
        by age;
        h.add();
    end ;

    h.output(dataset: cats("age_", age));
run;

Monday, June 6, 2022

Summary aggregate using Hash

Below are the key statements to understand hash "Maintaining Key Summaries". It is powerful technique when you understand how it works. However, avoid using SUMINC with MULTIDATA/DO_OVER as they are affecting the sum value and I can not find any explanation in SAS doc.

quote:
The summary value of a hash key is initialized to the value of the SUMINC variable whenever the ADD or REPLACE method is used.
The summary value of a hash key is incremented by the value of the SUMINC variable whenever the FIND, CHECK, or REF method is used.
Note that the SUMINC variable can be negative, positive, or zero valued. The variable does not need to be an integer.The SUMINC value for a key is zero by default.
unquote:
data sample;
    do key = 1,2,3;
        value = key*2;
        output; output;
    end;
run;

data _null_ ;
    if _n_ = 1 then do ;
        declare hash h (suminc:"value", dataset:"sample") ;
        h.defineKey("key") ;
        h.defineDone();

        do until (eof) ;
            set sample end=eof ;
            h.check();
        end;
    end ;

    file print ;

    set sample;
    by key;

    if first.key then do ;
        h.sum(sum:Total);
        put key @10 Total;
    end;
run;

Friday, November 29, 2019

How to loop character A-Z in SAS?

To loop characters A-Z elegantly, we can have two ways as follows:
data _null_;
    put "First method:";
    string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; * string from A to Z;
    do i=1 to length(string);
        char = substrn(string, i, 1);
        put char=; 
    end;

    put "Second method (using RANK and BYTE):";
    do i=rank('A') to rank('Z');
        char = byte(i);
        put char=;
    end;
run;
If you can find any way better, please let me know.