Statistical programming, matrix languages, and more

Saving matrices as sas datasets when using macro and do loop (SAS University Edition)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Saving matrices as sas datasets when using macro and do loop (SAS University Edition)

I construct 3 matrices of interest (av, fd0 and tcof / denoted V, F and B later) from 3 sas datasets (WIO, WFD and WVA) through using a macro and do loop below. I want to transform V, F and B to sas datasats in the end of the loop (highlighted in bold in the code below), but I am not able to do this. Any suggestions on what I can do to fix this?

 

The only error message I get in the log is:

WARNING: The quoted string currently being processed has become more than 262 bytes long. You
might have unbalanced quotation marks.

 

Also: when I try to print the matrices after running the code below (using: proc print data = F; runSmiley Wink nothing happens.

 

Code:

 

%Macro trade;
%do j = 2000 %to 2014;

data tmpi; set iiodat.WIO;
if year = &j;
run;

 

data va; set iiodat.WVA(keep = ctr sec year tva tout);
if year = &j;
run;

 

data invgo; set invout;
if year = &j;
run;

 

data fd0(drop = year); set iiodat.WFD;
if year = &j;
run;

 

data texpfd(drop=year); set iiodat.texpfgd;
if year = &j;
run;

 

data texpi(drop = year); set iiodat.texpint;
if year = &j;
if sec = "TOT" then delete;
run;

 

proc iml;
use tmpi(drop=year);
read all var _all_ into IO;
use va(keep=tout);
read all var _all_ into GO;
use va(keep=tva);
read all var _all_ into VA;
use invgo(keep = invgo);
read all var _all_ into invgo;
use fd0;
read all var _all_ into fd0;
use texpi(drop = texpi);
read all var _all_ into expi;
use texpfd(drop = texpfd domdmd);
read all var _all_ into expf;
use texpfd(keep = domdmd);
read all var _all_ into dfd;
use texpfd(keep = texpfd);
read all var _all_ into texpf;
use texpi(keep = texpi);
read all var _all_ into texpi;
end;

 

A = IO#t(invgo);
av = VA#(invgo);
I2464 =I(2464);

 

/* lieontif inverse */

tcof = inv(i2464-a);

 

store a av tcof fd0;

 

/* I denote fd0 = F, av = V and tcof= B */

proc iml;
create F from fd0;
append from fd0;
close F;
create V from av;
append from av;
close V;
create B from tcof;
append from tcof;
close B;
end;

 

data F&i;
run;

data V&i;
run;

data B&i;
run;

%end;
%mend trade;
%trade;

 

 

 

 

Log:

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
2 TITLE;
3 FOOTNOTE;
4 OPTIONS LOCALE=nb DFLANG=LOCALE;
WARNING: The quoted string currently being processed has become more than 262 bytes long. You
might have unbalanced quotation marks.
5 DATA _NULL_;
6 RUN;
7 OPTIONS VALIDVARNAME=V7;
8 FILENAME _HTMLOUT TEMP;
9 FILENAME _RTFOUT TEMP ENCODING='UTF-8';
10 FILENAME _PDFOUT TEMP;
11 FILENAME _GSFNAME TEMP;
12 FILENAME _DATAOUT TEMP;
13 %LET SYSCC=0;
14 %LET _CLIENTAPP=SAS Studio;
15 %LET _CLIENTAPPVERSION=3.5;
61 &GRAPHTERM; ;*';*";*/;RUN;QUIT;
______________
49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
release. Inserting white space between a quoted string and the succeeding identifier
is recommended.
 
16 %LET _SASPROGRAMFILE = %BQUOTE(/folders/myfolders/sasprogram4.sas);
17 %LET _BASEURL = %BQUOTE(http://localhost:10080/SASStudio/);
18 %LET _EXECENV=SASStudio;
19 DATA _NULL_;
20 CALL
20 ! SYMPUT("GRAPHINIT"
_
49
20 ! ,"");
21 CALL
21 ! SYMPUT("GRAPHTERM"
_
49
21 ! ,"");
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
release. Inserting white space between a quoted string and the succeeding identifier
is recommended.
 
22 RC=TSLVL('GEOCODE','N');
23 _ERROR_=0;
24 IF (RC^=' ') THEN DO;
25 CALL SYMPUT("GRAPHINIT","GOPTIONS RESET=ALL GSFNAME=_GSFNAME;");
___ ________________
49 49
26 CALL SYMPUT("GRAPHTERM","GOPTIONS NOACCESSIBLE;");
___ ___________________________________
49 49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
release. Inserting white space between a quoted string and the succeeding identifier
is recommended.
 
27 END;
28 RUN;
29 DATA _NULL_;
30 RC=SYSPROD("PRODNUM002"
_
49
30 ! );
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
release. Inserting white space between a quoted string and the succeeding identifier
is recommended.
 
31 IF (RC^=1) THEN DO;
32 CALL
32 ! SYMPUT("GRAPHINIT"
_
49
32 ! ,"");
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS
release. Inserting white space between a quoted string and the succeeding identifier
is recommended.
 
33 CALL SYMPUT("GRAPHTERM","");
34 END;
35 RUN;
36 %LET _DATAOUT_MIME_TYPE=;
37 %LET _DATAOUT_NAME=;
38 %LET _DATAOUT_TABLE=;
39 %LET _DATAOUT_URL=;
40 %SYMDEL _DATAOUT_MIME_TYPE _DATAOUT_NAME _DATAOUT_URL _DATAOUT_TABLE;
41 %LET _SASWS_ = %BQUOTE(/folders/myfolders);
42 %LET _SASWSTEMP_=%BQUOTE(/folders/myfolders/.images/8f8d00a9-0c97-4413-b01c-af46324a0b01)
42 ! ;
WARNING: The quoted string currently being processed has become more than 262 bytes long. You
might have unbalanced quotation marks.
43 ODS LISTING CLOSE;
44 OPTIONS PRINTERPATH=PDF;
45 ODS AUTONAVIGATE OFF;
46 ODS GRAPHICS ON;
47 ODS HTML5 (ID=WEB) DEVICE=PNG GPATH="&_SASWSTEMP_" ENCODING=utf8 FILE=_HTMLOUT
47 ! (TITLE='Results: sasprogram4.sas') STYLE=Htmlblue OPTIONS(BITMAP_MODE='INLINE'
47 ! SVG_MODE='INLINE' CSS_PREFIX='.ods_8f8d00a9-0c97-4413-b01c-af46324a0b01'
47 ! BODY_ID='div_8f8d00a9-0c97-4413-b01c-af46324a0b01' );
48 ODS RTF (ID=WEB) STYLE=Rtf FILE=_RTFOUT sasdate;
WARNING: The quoted string currently being processed has become more than 262 bytes long. You
might have unbalanced quotation marks.
49 ODS PDF (ID=WEB) STYLE=Pearl FILE=_PDFOUT;
50 &GRAPHINIT;
51 OPTIONS FIRSTOBS=1;
52 OPTIONS OBS=MAX;
53 OPTIONS DTRESET DATE NUMBER NOTES;
54 OPTIONS NOTES STIMER SOURCE NOSYNTAXCHECK;
55
56 libname IIOdat '/folders/myfolders';
57
58 options pagesize = 400;
59 options linesize = 100;
60 options missing = '.';
61
62 PROC IMPORT DATAFILE="/folders/myfolders/mapfile.xlsx"
63 OUT=ctrlabel
64 DBMS=xlsx
65 REPLACE;
66 Getnames=YES;
67 sheet=ctr;
68
69 proc sort; by ctr;
70 RUN;
71
72
73 /* avoid division by zero in matrix computation */
74
75 data invout; set iiodat.wva(keep = ctr sec year tout);
76 if tout > 0 then invgo = 1/tout;
77 else invgo = 0;
78 run;
79
80 data yearlabel(keep = ctr sec); set iiodat.wva;
WARNING: The quoted string currently being processed has become more than 262 bytes long. You
might have unbalanced quotation marks.
81 if year = 2000;
82 run;
83
84 /* Macro to decompose each country's gross exports at sector level */
85
86 %Macro trade;
87 %do j = 2000 %to 2014;
88
89 data tmpi; set iiodat.WIO;
90 if year = &j;
91 run;
92
93 data va; set iiodat.WVA(keep = ctr sec year tva tout);
94 if year = &j;
95 run;
96
97 data invgo; set invout;
98 if year = &j;
99 run;
100
101 data fd0(drop = year); set iiodat.WFD;
102 if year = &j;
103 run;
104
105 data texpfd(drop=year); set iiodat.texpfgd;
106 if year = &j;
107 run;
108
109 data texpi(drop = year); set iiodat.texpint;
110 if year = &j;
111 if sec = "TOT" then delete;
112 run;
113
114 proc iml;
115 use tmpi(drop=year);
116 read all var _all_ into IO;
117 use va(keep=tout);
118 read all var _all_ into GO;
119 use va(keep=tva);
120 read all var _all_ into VA;
121 use invgo(keep = invgo);
122 read all var _all_ into invgo;
123 use fd0;
124 read all var _all_ into fd0;
125 use texpi(drop = texpi);
WARNING: The quoted string currently being processed has become more than 262 bytes long. You
might have unbalanced quotation marks.
126 read all var _all_ into expi;
127 use texpfd(drop = texpfd domdmd);
128 read all var _all_ into expf;
129 use texpfd(keep = domdmd);
130 read all var _all_ into dfd;
131 use texpfd(keep = texpfd);
132 read all var _all_ into texpf;
133 use texpi(keep = texpi);
134 read all var _all_ into texpi;
135 end;
136
137 texp = expi + expf;
138
139 A = IO#t(invgo);
140 av = VA#(invgo);
141 I2464 =I(2464);
142
143 /* lieontif inverse */
144 tcof = inv(i2464-a);
145
146 store a av tcof fd0;
147
148 proc iml;
149 create F from fd0;
150 append from fd0;
151 close F;
152 create V from av;
153 append from av;
154 close V;
155 create B from tcof;
156 append from tcof;
157 close B;
158 end;
159
160 data F&i;
161 run;
162
163 data V&i;
164 run;
165
166 data B&i;
167 run;
168
169 %end;
170 %mend trade;
171 %trade;
172
173 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
174 ODS HTML CLOSE;
175 &GRAPHTERM; ;*';*";*/;RUN;QUIT;
176 QUIT;RUN;
177 ODS HTML5 (ID=WEB) CLOSE;
178
179 ODS RTF (ID=WEB) CLOSE;
180 ODS PDF (ID=WEB) CLOSE;
181 FILENAME _GSFNAME;
182 DATA _NULL_;
183 RUN;
184 OPTIONS NOTES STIMER SOURCE SYNTAXCHECK;

Accepted Solutions
Solution
‎12-14-2016 03:32 PM
SAS Super FREQ
Posts: 3,232

Re: Saving matrices as sas datasets when using macro and do loop (SAS University Edition)

The unbalanced quotes issue is probably from some previous drafts. Exit SAS and restart. In the future, you can submit the MAGIC STRING to eliminate close any open quotes/comments/etc.

 

In your program, I see at least two mistakes:

1. The END statement is used to complete a DO/END block.  Perhaps you are confusing END with QUIT, which exits PROC IML. 

2. When you type PROC IML, SAS exits any previous procedures and starts PROC IML in a fresh/new state.  In your program you use PROC IML before you attempt to write the SAS data sets.  That statement exits the previous program and clears memory, so the SAS data sets will not be written correctly. Delete the entire line just after the comment /* I denote fd0 = F, av = V and tcof= B */ . 

 

I'll add that you don't nee the macro at all.  SAS/IML can read multiple data sets in a single session, and supports where clauses so that you can read each year at a time. See

Read multiple data sets into SAS/IML

The WHERE clause in SAS/IML

and for a very advanced example

Search all variables in all data sets

 

Or you can keep the macro. Your choice! :-)

View solution in original post


All Replies
Solution
‎12-14-2016 03:32 PM
SAS Super FREQ
Posts: 3,232

Re: Saving matrices as sas datasets when using macro and do loop (SAS University Edition)

The unbalanced quotes issue is probably from some previous drafts. Exit SAS and restart. In the future, you can submit the MAGIC STRING to eliminate close any open quotes/comments/etc.

 

In your program, I see at least two mistakes:

1. The END statement is used to complete a DO/END block.  Perhaps you are confusing END with QUIT, which exits PROC IML. 

2. When you type PROC IML, SAS exits any previous procedures and starts PROC IML in a fresh/new state.  In your program you use PROC IML before you attempt to write the SAS data sets.  That statement exits the previous program and clears memory, so the SAS data sets will not be written correctly. Delete the entire line just after the comment /* I denote fd0 = F, av = V and tcof= B */ . 

 

I'll add that you don't nee the macro at all.  SAS/IML can read multiple data sets in a single session, and supports where clauses so that you can read each year at a time. See

Read multiple data sets into SAS/IML

The WHERE clause in SAS/IML

and for a very advanced example

Search all variables in all data sets

 

Or you can keep the macro. Your choice! :-)

Valued Guide
Posts: 505

Re: Saving matrices as sas datasets when using macro and do loop (SAS University Edition)

I put the following  magic string on function 12.
All I have to do is hit F12, highlight and hit RMB.
Takes about 1 second for me to fix the issue.

Works best in the more powerfull old text editor?, not EE, EG or SAS Studio

;;;;/*'*/ *);*};*];*/;/*"*/;%mend;run;quit;%end;end;run;endcomp;%utlfix;

%macro utlfix(dum);
* fix frozen sas and restore to invocation ;
 dm "odsresults;clear;";
ods results off;
 options ls=171 ps=65;run;quit;
 ods listing;
 ods select all;
 ods excel close;
 ods graphics off;
 proc printto;run;
 goptions reset=all;
 endsubmit;
 endrsubmit;
 run;quit;
 %utlopts;

%mend utlfix;
Valued Guide
Posts: 505

Re: Saving matrices as sas datasets when using macro and do loop (SAS University Edition)

As a side note
I always use the combination
run;quit;
for two reasons
It always works and will give an error for run group processing , which I can then fix.

1. If I highlight a datastep and RMB then I am assured it will run.

2. I am getting old and do not want to clutter my mind with 'less; useful commands.

I also always use 'cards4' and ';;;;' because it seems to always work and again I am getting old and rather not clutter my mind.

Occasional Contributor
Posts: 18

Re: Saving matrices as sas datasets when using macro and do loop (SAS University Edition)

Thank you for the quick reply! It made things much clearer. I solved the problem by changing the placement for END / QUIT and PROC IML. I decided to only look at one year (since not all 14 are of interest and I have limited time at the moment to discover new fancy comands), and thus also not to use a macro. The code is below, if interested.

 

Thank you again!

 


data tmpi; set iiodat.WIO;
if year = 2000;
run;

data va; set iiodat.WVA(keep = ctr sec year tva tout);
if year = 2000;
run;

data invgo; set invout;
if year = 2000;
run;

data fd0; set iiodat.WFD;
if year = 2000;
run;

data fd0; set fd0(drop=year);
run;

data texpfd; set iiodat.texpfgd;
if year = 2000;
run;

data texpfd; set texpfd(drop=year);
run;

data texpi; set iiodat.texpint;
if year = 2000;
if sec = "TOT" then delete;
run;

data texpi; set texpi(drop=year);
run;


proc iml;
use  tmpi(drop=year);
read all var _all_ into IO;
use va(keep=tout);
read all var _all_ into GO;
use va(keep=tva);
read all var _all_ into VA;
use invgo(keep = invgo);
read all var _all_ into invgo;
use fd0;
read all var _all_ into fd0;
use texpi(drop = texpi);
read all var _all_ into expi;
use texpfd(drop = texpfd domdmd);
read all var _all_ into expf;
use texpfd(keep = domdmd);
read all var _all_ into dfd;
use texpfd(keep = texpfd);
read all var _all_ into texpf;
use texpi(keep = texpi);
read all var _all_ into texpi;

 

texp = expi + expf;

A = IO#t(invgo);
av = VA#(invgo);
I2464 = I(2464);

/*Leontief inverse*/
tcof = inv(i2464-a);


store a av tcof fd0 dfd;

 

/* Create datasets from matrices */
create F from fd0;
append from fd0;
create av from av;
append from av;
create B from tcof;
append from tcof;
quit;

Post a Question
Discussion Stats
  • 4 replies
  • 237 views
  • 0 likes
  • 3 in conversation