Plotting trellis graphs with PROC PYTHON with SAS data

Philip Holland
Philip Holland Altair Community Member
edited June 2023 in Community Q&A

I'm trying to recreate some trellis graphs I've successfully created using PROC R, but with Python, and failing at the initial stage of importing SAS data sets into Python.

OPTIONS SET = PYTHONHOME 'F:\ProgramData\anaconda3';

DATA sasuser.v_prdsale / VIEW = sasuser.v_prdsale;
SET sashelp9.prdsale;
LENGTH yyq $6;
yyqtr = year + (quarter - 1) / 4;
mon = MONTH(month);
yyq = PUT(month, YYQ6.);
yq = INTCK('QTR', '31dec1992'd, month);
SELECT (country);
WHEN ('U.S.A.') cntry = 'USA';
WHEN ('GERMANY') cntry = 'DE';
WHEN ('CANADA') cntry = 'CA';
OTHERWISE;
END;
RUN;

PROC SUMMARY DATA = sasuser.v_prdsale MISSING NWAY;
CLASS cntry yq product;
VAR actual;
OUTPUT OUT = prdsale (DROP = _:) SUM=;
FORMAT _ALL_;
RUN;

PROC PRINT DATA = prdsale;
TITLE 'PRDSALE summary';
RUN;

ODS ESCAPECHAR='^';

ODS HTML FILE = "./python_plotly_WPS.html" STYLE=minimal GTITLE GFOOTNOTE GPATH="./images";

PROC PYTHON TERMINATE;
EXPORT DATA = prdsale PYTHON = sasdata;
SUBMIT;
print(sasdata)
ENDSUBMIT;
RUN;

ODS HTML CLOSE;

What I get are the following Python messages:

NOTE: Submitting statements to Python:

NOTE: File "<string>", line 2

NOTE: print(sasdata)

NOTE: IndentationError

NOTE: :

NOTE: unexpected indent

The SAS data set converted to the Python dataframe is:

cntry  yq  PRODUCT  ACTUAL 

CA    1    BED    4337
CA    1    CHAIR    5115
CA    1    DESK    6644
CA    1    SOFA    6931
CA    1    TABLE    5977
CA    2    BED    6459
CA    2    CHAIR    4334
CA    2    DESK    6906
CA    2    SOFA    7505
CA    2    TABLE    5611
CA    3    BED    5199
CA    3    CHAIR    7908
CA    3    DESK    5441
CA    3    SOFA    6628
CA    3    TABLE    5870
CA    4    BED    5849
CA    4    CHAIR    7105
CA    4    DESK    6684
CA    4    SOFA    5656
CA    4    TABLE    4861
CA    5    BED    6787
CA    5    CHAIR    6888
CA    5    DESK    6668
CA    5    SOFA    5532
CA    5    TABLE    5425
CA    6    BED    7377
CA    6    CHAIR    7275
CA    6    DESK    6408
CA    6    SOFA    6514
CA    6    TABLE    7000
CA    7    BED    7207
CA    7    CHAIR    5219
CA    7    DESK    5538
CA    7    SOFA    5544
CA    7    TABLE    6345
CA    8    BED    4514
CA    8    CHAIR    6395
CA    8    DESK    7898
CA    8    SOFA    5825
CA    8    TABLE    5611
DE    1    BED    5026
DE    1    CHAIR    6276
DE    1    DESK    4330
DE    1    SOFA    8915
DE    1    TABLE    5761
DE    2    BED    6511
DE    2    CHAIR    6098
DE    2    DESK    7207
DE    2    SOFA    7272
DE    2    TABLE    7792
DE    3    BED    6887
DE    3    CHAIR    4697
DE    3    DESK    7743
DE    3    SOFA    8267
DE    3    TABLE    5039
DE    4    BED    5094
DE    4    CHAIR    6074
DE    4    DESK    5878
DE    4    SOFA    6274
DE    4    TABLE    6263
DE    5    BED    6172
DE    5    CHAIR    5583
DE    5    DESK    5038
DE    5    SOFA    6215
DE    5    TABLE    7182
DE    6    BED    4568
DE    6    CHAIR    7661
DE    6    DESK    4447
DE    6    SOFA    6720
DE    6    TABLE    5616
DE    7    BED    4943
DE    7    CHAIR    5333
DE    7    DESK    6397
DE    7    SOFA    4600
DE    7    TABLE    7231
DE    8    BED    6933
DE    8    CHAIR    5383
DE    8    DESK    7462
DE    8    SOFA    6797
DE    8    TABLE    4313
USA    1    BED    4665
USA    1    CHAIR    5709
USA    1    DESK    7003
USA    1    SOFA    5563
USA    1    TABLE    7018
USA    2    BED    7371
USA    2    CHAIR    4435
USA    2    DESK    7027
USA    2    SOFA    4160
USA    2    TABLE    7115
USA    3    BED    5895
USA    3    CHAIR    7609
USA    3    DESK    5829
USA    3    SOFA    5732
USA    3    TABLE    4690
USA    4    BED    6170
USA    4    CHAIR    7660
USA    4    DESK    5475
USA    4    SOFA    6529
USA    4    TABLE    5398
USA    5    BED    4562
USA    5    CHAIR    5706
USA    5    DESK    7238
USA    5    SOFA    5071
USA    5    TABLE    5696
USA    6    BED    6137
USA    6    CHAIR    6344
USA    6    DESK    4985
USA    6    SOFA    6138
USA    6    TABLE    6169
USA    7    BED    6368
USA    7    CHAIR    7469
USA    7    DESK    5845
USA    7    SOFA    6363
USA    7    TABLE    4647
USA    8    BED    7006
USA    8    CHAIR    6004
USA    8    DESK    5141
USA    8    SOFA    3837
USA    8    TABLE    5570

Best Answer

  • Geoff Mole
    Geoff Mole
    Altair Employee
    edited June 2023 Answer ✓

    Hi Philip,

    I suspect because you are getting an unexpected indent error in your PYTHON print(sasdata) line there could be some hidden white space or TAB character preceding the print(sasdata). I can easily reproduce your issue with any SAS dataset if I place a tab character before the print (sasdata) within the SUBMIT block.

    Perhaps when you copy and paste from whatever text editor you are using to create this program into this kind of web forum, this type of visible indentation has been lost and is not a true reflection of the code. Given that PYTHON is an indentation oriented programming language I guess it would not be unusual for programmers to start indenting PYTHON code as you might in a native PYTHON coding environment, but that is a different philosophical discussion. Either way my advice would be to check carefully and remove any hidden characters between the end of SUBMIT; and the beginning of print(sasdata) and try again.

    Regards,

    Geoff Mole

     

Answers

  • Geoff Mole
    Geoff Mole
    Altair Employee
    edited June 2023 Answer ✓

    Hi Philip,

    I suspect because you are getting an unexpected indent error in your PYTHON print(sasdata) line there could be some hidden white space or TAB character preceding the print(sasdata). I can easily reproduce your issue with any SAS dataset if I place a tab character before the print (sasdata) within the SUBMIT block.

    Perhaps when you copy and paste from whatever text editor you are using to create this program into this kind of web forum, this type of visible indentation has been lost and is not a true reflection of the code. Given that PYTHON is an indentation oriented programming language I guess it would not be unusual for programmers to start indenting PYTHON code as you might in a native PYTHON coding environment, but that is a different philosophical discussion. Either way my advice would be to check carefully and remove any hidden characters between the end of SUBMIT; and the beginning of print(sasdata) and try again.

    Regards,

    Geoff Mole

     

  • Philip Holland
    Philip Holland Altair Community Member
    edited June 2023

    Thanks Geoff,

    I've lived in a world of irrelevant white-space for so long I'd forgotten that some languages still require statements to start in column 1. SAS and R seemed to be tolerant of leading spaces (although I remember IBM mainframe SAS would terminate if you started a line with /* in column 1!), but Python is not that tolerant.

    My sample code (without the leading spaces) now works as expected, and my plotly.express facet plot behaves well without the leading spaces too. It doesn't look much like the equivalent R xyplot trellis plot, but at least it shows the same data.

    Many thanks.............Phil