Error when exporting from SAS to Excel - comma problem - excel

so I have a code like this, when this executes
data t4;
put new_EMPLID $2000.;
do until(last.CRSE_ID);
set t3;
by CRSE_ID notsorted;
new_EMPLID = catx(',',new_EMPLID,compress(EMPLID));
END;
drop EMPLID;
run;
output on SAS is
THEN "View in Excel"
I want to show LIKE THIS "2234944,2330002" exactly like output on SAS
Why excel is changing like pic? Does anybody know how to fix this issue???
I know , is cauing issue, when I changed to 'x', it worked on excel.

What operating system, SAS version and Excel version and bittiness of each ?
What code did you submit ?
This sample code with Proc EXPORT exhibits no issues in Windows 10, SAS 9.4M4 and Excel 2016 64-bit.
If a courseId has only one attendee, or the if commas could be misinterpreted by Excel they will be :). Your language settings can also have influence the Excel rendering.
data have; input
courseId employeeId; datalines;
1 2234944
1 2330002
1 1975365
1 2244221
2 1122334
2 2233445
2 3344551
run;
data want;
do until (last.courseId);
set have;
by courseId;
length attendeeList $2000;
attendeeList = catx(',',attendeeList,employeeId);
end;
drop employeeId;
run;
proc export data=want dbms=excel replace file='c:\temp\course_attendence.xlsx';
run;

Related

SAS SGPLOT Meaningful names for charts with by statement

I refer here to the the issue addressed 6 years ago in "SAS GRAPH and multiple charts, more meaningful names?" To me this issue needs a solution. I need to generate one chart by indicator (2) and country (27). The index is 1,3,5,7,9,...,107. Please try to find the file/worksheet (png/xls) for country x and indicator y. Good luck! Can somebody help me? Maybe the person who addressed the issue at that time found a solution (I really hope it)? Many thanks!
Below you have an example using sashelp library. It creates Picture1.png, Picture3.png and Picture5.png. Instead of this, I would like to have PictureIBM.png, PictureIntel.png and PictureMicrosoft.png.
proc sort data=SASHELP.STOCKS out=testfile; by stock date; run;
data testfile; set testfile;
by stock ;
retain n;
if first.stock then n=0; else n=n+1;
if n le 5;
run;
ods listing gpath='write the path' ;
ods listing /*style = styles.new_font */
gpath='/ec/prod/1eusilc/flashestimates/FLIPI/3_Output/FE/PERFORMANCE/overall 2022/TIME SERIES' ;
ods graphics on /
width=14in height=10in
outputfmt=gif
imagemap=on
imagename="Picture"
border=off reset=index ;
proc sgplot data=testfile noborder noopaque nowall dattrmap=DATTRMAP
title "write the title";
by stock ;
highlow x=date low=low high=high / type=bar ;
scatter x=date y=close ;
xaxis type=discrete;
run;
Here are two ways to do this, one uses the #byval as indicated in the previous solution and which you used in title but not the file name for some reason.
First make fake data:
data prdsummary;
set sashelp.prdsale;
where year=1993 and (country="GERMANY" or country="CANADA") and region="EAST"
and division="CONSUMER" and
(product="SOFA" or product="TABLE" or product="BED");
run;
proc sort data=prdsummary;
by country;
run;
Then use #BYVAL. However, if you run this code multiple times, SAS does not overwrite the previous files, it adds a 1 to the filename instead. So you need to make sure that the files don't already exist. There may be an option to control this feature, I'm unsure at the moment.
ods listing gpath='/home/fkhurshed/Demo1/';
title 'Graph for #byval1';
ods graphics / imagename="Country #byval1";
proc sgplot data=prdsummary;
by country;
vbar prodtype / group=product response=actual groupdisplay=stack;
run;
A second workaround is to use a macro instead and control the name explicitly.
*explicitly control the names via macro looping;
ods listing gpath='/home/fkhurshed/Demo1/';
%macro generate_country_graph(country=);
ods graphics / imagename="Graphic for &Country.";
proc sgplot data=prdsummary;
where country="&country";
vbar prodtype / group=product response=actual groupdisplay=stack;
run;
%mend;
data _null_;
set prdsummary;
by country;
if first.country then
do;
str=catt('%generate_country_graph(country=', country, ');');
call execute(str);
end;
run;

How to make PC SAS DDE Sever Code to communicate with Windows 10 Excel 2016

I had set up a code in PC SAS in my Windows 7 having Excel 2010 and PC SAS 9.4. Basically what the code did was take the dataset 'wide2' and paste it to the only excel file which is open in the OS at that moment and then save it. Following is the code which I was using:
%macro paste(number);
proc print data=wide2;
run;
PROC CONTENTS DATA=wide2 /*NOPRINT*/ OUT=CNT ;
RUN;
PROC SORT DATA=CNT ;
BY VARNUM ;
RUN;
PROC SQL /*NOPRINT*/;
SELECT NAME
INTO: VARS SEPARATED BY ' '
FROM CNT ;
SELECT COUNT(DISTINCT NAME)
INTO: COLS SEPARATED BY ' '
FROM CNT ;
SELECT NOBS
INTO: ROWS
FROM CNT
WHERE VARNUM = 1;
QUIT;
proc print data=cnt;run;
FILENAME TEMP DDE "EXCEL|Sheet&number.!R1C1:R200C200" ;
data _null_ ;
file temp ;
if _n_=1 then do;
do _n_=1 to &cols ;
set cnt(keep=name rename=(name=__name__)) ;
put __name__ #;
end;
put;
end;
set wide2 ;
put &vars ;
run ;
%mend paste;
So when I executed a macro like %paste(1), it would paste the data to Excel's Sheet1.
Now we have moved to Windows 10 which has the same PC SAS version but Excel 2016.
When I run the same code with the same dataset, the PC SAS gets hung for an eternity after a couple of seconds and then I have to just kill the process. Nothing happens to the open excel file. I can't even see the log since the PC SAS gets hung. Please suggest a solution for this.( The top bar of the PCSAS when it gets hung says 'running DATA Step')
How many columns and rows in the wide2 data set ? Your posted code ran on my system with SAS 9.4M4 and Office 365. I used this data set.
data wide2;
do id=1 to 3;
array x(15) (1:15);
output;
end;
format _numeric_ 2.;
run;

No visual content after running report in Excel

I am trying to run a Stored Process from the SAS tab in Excel (Reports button). After running it, I get the following message: "Stored Process has completed with no visual content. You can refresh this analysis through the Manage Content dialog box". Below you can see my code:
ods excel;
create table DAI as
select DISTINCT (A.V_NUMBER), B.PHRDEDN_APPLICABLE_GROSS
from UVICRPT.V_HRS_PAYROLL_PHRHIST A, UVICRPT.V_HRS_PAYROLL_PHRDEDN B
WHERE A.PHRHIST_DISP in ('70')
AND A.PHRHIST_PAYNO = &PayNo AND B.PHRDEDN_PAYNO = &PayNo
AND A.PHRHIST_PICT_CODE = "&PictCode" AND B.PHRDEDN_PICT_CODE = "&PictCode"
AND A.PHRHIST_YEAR = B.PHRDEDN_YEAR
AND A.PHRHIST_SEQ_NO = B.PHRDEDN_SEQ_NO;
run;
ods excel options(sheet_interval="table" sheet_name="Koala");
proc print data=DAI style=Table noobs;
run;
ods excel close;
QUIT;
All I want is a new sheet in the book I'm working on named "Koala" with the table displayed.
What am I doing wrong? Any help?

How to open .sas7bdat in SAS and export to excel?

I am quite new to SAS. I have got a file with file extension .sas7bdat which contains daily stocks prices and percentage changes. It has almost 2 million line items. I know that I can simply double click the file and open it with SAS 9.4. But, I am looking for codes which I can type in Editor and open this file. Please help me.
After I open this file, I need to export it to excel. Since it has 2 million data, I can not export everything in a single excel tab. So, What I want to do it randomly pick (say 10,000 or 20,000) data and export only this randomly picked data to excel.
My .sas7bdat file is on desktop.
Please help.
You can use surveyselect and specify the number of records you want in your subset the use proc export.
In my Example below I create a table of 10 rows and only wanted 5 row in the subset. just change the value in my macro variable from 5 to 100000
Code:
data have;
input value;
datalines;
1
2
3
4
5
6
7
8
9
10
;
run;
%let subset=5;
proc surveyselect data=have
method=srs n=&subset. out=want;
run;
Output:
value=1
value=2
value=5
value=6
value=10
Exporting:
proc export data=sashelp.class
outfile='c:\myfiles\want.csv'
dbms=csv
replace;
run;
You can also filter on the data you are exporting, dummy example below:
proc export data=want (where=(value > 100 or location='X'))
outfile='c:\myfiles\want.csv'
dbms=csv
replace;
run;
You can use ODS. This will be simpler, but generate a file which will give warning in opening first time
libname rd_data "<Your Path to dataset>"
data temp;
set rd_data.<dataset name>;
rnd = ranuni(123456);
run;
proc sort data = temp out = temp(drop=rnd);
by rnd;
run;
**** Remember this is .xls file, not .xlsx
ods html file = <xls file path - full path>;
proc print data = temp(obs=1000);
run;
ods html close;
Alternatively, you can use DDE (Dynamic Data Exchange)
First, create a library to point to the location on the file system where the data set resides. This is a pointer (in C terms) to the directory.
libname myData "<path to folder>";
From there you can use a random number and a data step to get N random values. Alternatively, PROC SURVEYSELECT can be used, but you might not have it licensed.
data temp;
set myData.<Data Set Name>;
__rnd = ranuni(1);
run;
proc sort data=temp ;
by __rnd;
run;
data toOutput;
set temp(obs=10000 drop=__rnd);
run;
The last Data Step reads in just the first 10,000 records which you randomized above.
Then you can use PROC EXPORT to export the values.
proc export data=toOutput outfile="c:\temp\output.xlsx" dbms=xlsx replace;
sheet="MyData";
run;
The great thing here is that you can create other sheets in the file with additional exports.
proc export data=toOutput outfile="c:\temp\output.xlsx" dbms=xlsx replace;
sheet="MyData2";
run;
This would allow you to create multiple samples or even export all the data across multiple sheets.

Change sheet names in SAS ODS dynamically

Is there a way to dynamically change the sheet names in SAS ODS using macros? Inside a macro, I have the following code:
%DO N = 1 %TO &SQLOBS;
ODS TAGSETS.ExcelXP
OPTIONS(SHEET_NAME = &&TABLEVAR&N
EMBEDDED_TITLES = 'NO'
EMBEDDED_FOOTNOTES = 'NO'
PRINT_HEADER = 'CURRENT &&TABLEVAR&N AS OF &D';
PROC PRINT
DATA = WORK.&&TABLEVAR&N
NOOBS;
RUN;
%END;
Which basically loops through an array of table names and for each table outputs a separate Excel Sheet. And by calling a macro variable &TABLEVAR, it is supposed to dynamically change the sheet name for each table. However, I get the following error:
ERROR 22-322: Expecting a quoted string.
So SAS does resolve the macro variable, I can see the table names are right in the log, but the problem is they are not a quoted string. So I tried it like this with quotes around it:
OPTIONS(SHEET_NAME = '&&TABLEVAR&N'
But then the macro variable is not being resolved by SAS and all the sheets are named &&TABLEVAR&1, 2, etc.
Is there a way around this? (And each sheet is a separate table with a different name so I can't use a BY group.) What good macros are if they can't interact with ODS?
I just found out placing %sysfunc and quote in front of &&TABLEVAR&N does the trick.
So the final code looks like this:
%DO N = 1 %TO &SQLOBS;
ODS TAGSETS.ExcelXP
OPTIONS(SHEET_NAME = %sysfunc(quote(&&TABLEVAR&N))
EMBEDDED_TITLES = 'NO'
EMBEDDED_FOOTNOTES = 'NO'
PRINT_HEADER = 'CURRENT &&TABLEVAR&N AS OF &D';
PROC PRINT
DATA = WORK.&&TABLEVAR&N
NOOBS;
RUN;
%END;
I don't think you need all that. If you use double quotes around the macro variable expression, you should get the same result as the %SYSFUNC(QUOTE()) function:
"&&TABLEVAR&N"
That is, unless you somehow plan to have double quotes in the sheet names....

Resources