Jupyter notebook display SAS output word-wrapper - sas

I have a table in sas format (.sas7bdat) and would like to output it in Jupyter notebook.
proc print data=dataBoxE.my_data (firstobs=2 obs=12);
run;
The output table is jammed together since it has 100+ columns. How should I setup the environment within my notebook?
Moreover, is there a way to save the log file instead of opening it right away in the output cell? Thanks.

In SAS you can change the location of where the log file is created using proc printto; Documentation here.
When using proc printto, don't forget to reset the location to the default system value at the end of your, Example:
proc printto log='c:\em\log1.log';
run;
/* Your code here */
proc printto;
run;
If you don't need the 100+ columns; then select only the ones you want using the VAR statement in proc print Documentation here :
proc print data=exprev;
var country price sale_type;
run;
If you want all the 100+; just export them to csv using proc export and view them in any spreadsheet reader to avoid crashing your browser. Documentation here.
proc export data=sashelp.class
outfile='c:\myfiles\Femalelist.csv'
dbms=csv
replace;
run;

Related

Can I use wildcards in dataset names for PROC CONTENTS?

On the SAS server we have a library that contains thousands of datasets. I want to catalog the contents of a subset of these, all of which have names that begin with "prov". Can I use a wildcard to specify this?
I tried:
PROC CONTENTS DATA=library.prov*;
RUN;
But that just produces a log with this error message:
ERROR: File LIBRARY.PROV.DATA does not exist.
I also tried library.prov%, and that gave the same error.
There are over 100 datasets that start with "prov" so I really don't want to have to do them one at a time. Any ideas?
Depending on what information you want that the CONTENTS procedure produces you could just use the DICTIONARY metadata views.
proc sql ;
create table want as
select *
from dictionary.columns
where libname = 'LIBREF'
and memname like 'PROV%'
;
quit;
Use a WHERE data set option.
proc contents data=sashelp._all_ noprint out=class(where=(memname like 'CLASS%'));
run;
When you specify the keyword _ALL_ in the PROC CONTENTS statement, the step displays a list of all the SAS files that are in the specified SAS library.
Example :
PROC CONTENTS DATA=libref._ALL_ NODS;
RUN;
But to open only the datasets that begin with prov you can use the SQL and add CONTAINS to WHERE e.g:
proc sql ;
create table mytables as
select *
from dictionary.tables
where libname = 'WORK'
order by memname ;
quit ;
Now just run:
PROC CONTENTS DATA mytables;
RUN;
I may be using a different version of SAS check if you have the library SASHELP if so try this based on my note in your comment on the previous response you may see that this works out for you:
proc sql outobs=100;
create table see as
select distinct libname,memname,crdate,modate from sashelp.vtable
where libname='LIBRARY' and memname like 'PROV%'
order by memname;
quit;

SAS ODS Query/Statement print along with it's output

SAS EG
Is there any way I can print the query/statement used to get the output, along with the output, using SAS ODS?
Suppose,
ods pdf file=pdfile;
proc sql;
select a.*
from tab1 a inner join tab2 b
on a.something=b.something
where <>
having <>;
quit;
ods _all_ close;
this would print the OUTPUT generated from the above query. But can I also get the query printed via the ods pdf along with the output?
There's no automatic way to redirect the log that I'm aware of.
There are a few ways to get what you want, however.
First off, if you are able to use Jupytr, SAS has plugins to enable that to work with SAS, and then you can simply write in the notebook and run the code, and the results appear with your code just as you want. See Chris Hemedinger's blog post on the subject for more details.
Second, SAS Studio will support a notebook-style interface probably with the next major revision (I believe version 5.0) which will release late next year. So similarly, you would put your code and get your output in the same windows.
Finally, the third option is to do as Reeza suggested - write to a log file, then print that to the output. It's messy but possible.
Here's an example of the latter. I don't make any effort to clean it up, note, you'd probably want to remove the logging related to PROC PRINTTO and the otehr notes (or turn on NONOTE).
ods pdf file="c:\temp\test.pdf";
filename logfile temp;
proc printto log=logfile;
run;
proc sql;
select * from sashelp.class;
quit;
proc printto;
run;
data _null_;
infile logfile;
input #1 #;
call execute(cats('ods text="',trim(_infile_),'";'));
run;
ods _all_ close;

Exporting SAS data into SPSS with value labels

I have a simple data table in SAS, where I have the results from a survey I sent to my friends:
DATA Questionnaire;
INPUT make $ Question_Score ;
CARDS;
Ned 1
Shadowmoon 2
Heisenberg 1
Athelstan 4
Arnold 5
;
RUN;
What I want to do, using SAS, is to export this table into SPSS (.sav), and also have the value labels for the Question_Score, like shown in the picture below:
I then proceed to create a format in SAS (in hope this would do it):
PROC FORMAT;
VALUE Question_Score_frmt
1="Totally Agree"
2="Agree"
3="Neutral"
4="Disagree"
5="Totally Disagree"
;
run;
PROC FREQ DATA=Questionnaire;
FORMAT Question_Score Question_Score_frmt.
;
TABLES Question_Score;
RUN;
and finally export the table to a .sav file using the fmtlib option:
proc export data=Questionnaire outfile="D:\Questionnaire.sav"
dbms=spss replace;
fmtlib=work.Q1frmt;
quit;
Only to disappoint myself seeing that it didn't work.
Any ideas on how to do this?
You didn't apply the format to the dataset, unfortunately, you applied it to the proc freq. You would need to use PROC DATASETS or a data step to apply it to the dataset.
proc datasets lib=work;
modify questionnaire;
format Question_Score Question_Score_frmt.;
run;
quit;
Then exporting will include the format, if it's compatible in SAS's opinion with SPSS's value label rules. I will note that SAS's understanding of SPSS's rules is quite old, based on I think SPSS version 9, and so it's fairly often that it won't work still, unfortunately.

How do I import a census table into SAS when the variable name has special characters in it?

I am using SAS Enterprise Guide, importing American Community Survey tables from the census into a script to work with them. Here is an example of a raw census csv I'm importing into SAS Enterprise Guide:
within my data step, when I use the command
County=Geo.display-label;
I get this error:
In base SAS, I was using
County=Geo_display_label;
While that worked in base SAS, when I tried that in Enterprise Guide, I got this error:
What is a way to get the raw data's variable name Geo.display-label to read into SAS Enterprise Guide correctly?
To see the impact of the VALIDVARNAME option on the names that PROC IMPORT generates when the column headers are not valid SAS names lets make a little test CSV file.
filename csv temp ;
data _null_;
file csv ;
put 'GEO.id,GEO.id2,GEO.display-label';
put 'id1,id2,geography';
run;
If we run PROC IMPORT to convert that into a SAS datasets when VALIDVARNAME option is set to ANY then it will use the column headers exactly, including the illegal characters like period and hyphen. To reference the variables with those illegal characters we will need to use name literals.
options validvarname=any;
proc import datafile=csv replace out=test1 dbms=dlm;
delimiter=',';
run;
proc contents data=test1; run;
proc freq data=test1;
tables 'GEO.display-label'n ;
run;
But if we set the option to V7 instead then it will convert the illegal characters into underscores.
options validvarname=v7;
proc import datafile=csv replace out=test2 dbms=dlm;
delimiter=',';
run;
proc contents data=test2; run;
proc freq data=test2;
tables geo_display_label ;
run;
County = 'geo.display-label'n;
if you set OPTIONS VALIDVARNAME=V7; in EG you will get the same names as batch sas.

combine proc sql step in proc report to create report in excel

ods html file = "Y:/cars/cars.xls";
proc sql;
title "Cars";
select
make,model,type,origin,drivetrain
from sashelp.class
where engine size gt 3;
quit;
Suppose after run above query in SAS I will get output in excel format.
So, how it is possible to get same ouput via proc report step?
You wouldn't get much output from that since there aren't any cars in sashelp.class.
The basic proc report step will look something like:
title "Cars";
proc report data=sashelp.cars nowd;
where enginesize gt 3;
columns make model type origin drivetrain;
run;
I wouldn't use PROC REPORT though just to do this - the point of PROC REPORT is all of the other features you can use, between the formatting options, the summarization options, etc.