BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ak2011
Fluorite | Level 6

Hello,

I posted this same macro question in my previous post and 2 people made comments on that. I have corrected the work based on their comments but the problem persists; I am unable to see the results of the macro and the log gives no error message.

Help needed, please.

 

My data, code and log are given below:

Thans for in advance for your help. 

ak.

 


/***MACRO*/

data full_tdata;
input id$ 1-6 job 7-8 jobyrin 9-11 jobyrout 18-20 icode$ 23-27 lung$ 29-35;
datalines;
OSa13 3 73 78 6191 cacase
OSa30 1 39 46 7181 cacase
OSa30 3 56 64 6191 cacase
OSa73 1 23 31 7181 popcon
OSa73 2 31 42 5130 popcon
OSa86 3 46 60 6198 cacon
OSa86 4 60 70 5130 cacon
OSa93 3 68 72 6121 popcon
OSf26 1 54 54 6198 popcon
OSf26 2 70 70 6191 popcon
OSh77 1 63 66 6121 cacon
OSh77 2 66 70 6121 cacon
OSh77 3 70 71 6121 cacon
OSi84 1 67 75 6191 cacase
OSi84 2 75 81 6191 cacase
OSj17 3 60 75 6191 cacase
OSj17 4 65 70 6191 cacase
;

proc print data=full_tdata;

data small_tdata; set full_tdata; drop lung;

proc freq data=small_tdata;
tables job; run;

proc print data=small_tdata;
run;


/* MACRO TEST*/

*max 4 jobs per subject;


%macro duration(database=small_tdata, icode=icode, Job_title="baker");

*Creating a dataset containing all the jobs with selected icode;

data &Job_title;
set &database;
if icode = "&6191";
run;


*transposing dataset so that the YEARIN and YEAROUT of all jobs held by a subject are in the same row;


*YEARIN;

proc transpose data = &Job_title out = &Job_title._yearin prefix = YEARIN;
by ID;
var jobyrin;
run;

proc sort data = &Job_title._yearin;
by id;
run;

*YEAROUT;

proc transpose data = &Job_title out = &Job_title._yearout prefix = YEAROUT;
by ID;
var jobyrout;
run;

proc sort data = &Job_title._yearout;
by id;
run;



data &Job_title._final;
merge &Job_title._yearin &Job_title._yearout;
by ID;

array YEARIN (3) YEARIN1 YEARIN2 YEARIN3;
array YEAROUT (3) YEAROUT1 YEAROUT2 YEAROUT3;

array YEARINv2 (3) YEARIN2 YEARIN3 YEARIN4 ;
array YEAROUTv2 (3) YEAROUT2 YEAROUT3 YEAROUT4;

array YEARIN_F (4) YEARIN1 YEARIN2 YEARIN3 YEARIN4;
array YEAROUT_F (4) YEAROUT1 YEAROUT2 YEAROUT3 YEAROUT4;

Array duration (4) Dur1 Dur2 Dur3 Dur4;



do i = 1 to 3;

if yearin(i) ne . and yearinv2(i) ne . and yearinv2(i) le yearout(i) and yearoutv2(i) ge yearout(i) then do; yearinv2(i) = yearin(i); yearin(i) = .;yearout(i) = .;end;
else if yearin(i) ne . and yearinv2(i) ne . and yearinv2(i) le yearout(i) and yearoutv2(i) lt yearout(i) then do; yearinv2(i) = yearin(i);yearoutv2(i) = yearout(i); yearin(i) = .;yearout(i) = .;end;

end;

do i = 1 to 4;

if YEARIN_F(i) = . then YEARIN_F(i) = 0;
if YEAROUT_F(i) = . then YEAROUT_F(i) = 0;

duration(i) = YEAROUT_F(i) - YEARIN_F(i);
if YEARIN_F(i) ne 0 and YEAROUT_F(i) = YEARIN_F(i) then duration(i) = 0.5;
end;

&Job_title._dur = Dur1 + Dur2 + Dur3 + Dur4;

&Job_title._ever = 1;

keep ID &Job_title._dur &Job_title._ever;
run;

/* Display the macro results*//*????? I am stuck here, please*/

%duration(database=small_tdata,icode=icode, Job_title=baker);

proc print data=&Job_title_final;
run;

%MEND duration;
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
2 TITLE;
3 FOOTNOTE;
4 OPTIONS LOCALE=en_US DFLANG=LOCALE;
5 DATA _NULL_;
6 RUN;
7 OPTIONS VALIDVARNAME=V7;
8 OPTIONS VALIDMEMNAME=EXTEND;
9 FILENAME _HTMLOUT TEMP;
10 FILENAME _RTFOUT TEMP ENCODING='UTF-8';
11 FILENAME _PDFOUT TEMP;
12 FILENAME _GSFNAME TEMP;
13 FILENAME _DATAOUT TEMP;
14 %LET SYSCC=0;
15 %LET _CLIENTAPP='SAS Studio';
16 %LET _CLIENTAPPABREV=Studio;
17 %LET _CLIENTAPPVERSION=3.8;
18 %LET _CLIENTVERSION=3.8;
19 %LET _CLIENTMODE=basic;
20 %LET _SASSERVERNAME=%BQUOTE(localhost);
21 %LET _SASHOSTNAME=%BQUOTE(localhost);
22 %LET _SASPROGRAMFILEHOST=%BQUOTE(localhost);
23 %LET _CLIENTUSERID=%BQUOTE(sasdemo);
24 %LET _CLIENTUSERNAME=%BQUOTE(sasdemo);
25 %LET CLIENTMACHINE=%BQUOTE(10.0.2.2);
26 %LET _CLIENTMACHINE=%BQUOTE(10.0.2.2);
27 %let SASWORKLOCATION="%sysfunc(getoption(work))/";
28 FILENAME _CWD '.';
29 DATA _NULL_;
30 CALL SYMPUT('_SASWORKINGDIR',PATHNAME('_CWD'));
31 RUN;
32 FILENAME _CWD;
33
34 %LET _SASPROGRAMFILE = %NRQUOTE(%NRSTR(/folders/myfolders/multisitedata/Occup_Macro_ZM19.sas));
35 %LET _BASEURL = %BQUOTE(http://localhost:10080/SASStudio/);
36 %LET _EXECENV=SASStudio;
37 DATA _NULL_;
38 CALL SYMPUT("GRAPHINIT","");
39 CALL SYMPUT("GRAPHTERM","");
40 RC=TSLVL('SASXGOPT','N');
41 _ERROR_=0;
42 IF (RC^=' ') THEN DO;
43 CALL SYMPUT("GRAPHINIT","GOPTIONS RESET=ALL GSFNAME=_GSFNAME;");
44 CALL SYMPUT("GRAPHTERM","GOPTIONS NOACCESSIBLE;");
45 END;
46 RUN;
47 DATA _NULL_;
48 RC=SYSPROD("PRODNUM002");
49 IF (RC^=1) THEN DO;
50 CALL SYMPUT("GRAPHINIT","");
51 CALL SYMPUT("GRAPHTERM","");
52 END;
53 RUN;
54 %LET _DATAOUT_MIME_TYPE=;
55 %LET _DATAOUT_NAME=;
56 %LET _DATAOUT_TABLE=;
57 %LET _DATAOUT_URL=;
58 %SYMDEL _DATAOUT_MIME_TYPE _DATAOUT_NAME _DATAOUT_URL _DATAOUT_TABLE;
59 %LET _SASWS_ = %BQUOTE(/folders/myfolders);
60 %LET _SASWSTEMP_=%BQUOTE(/folders/myfolders/.sasstudio/.images/717c6288-07d7-4390-9c85-69b0718d63f4);
61 ODS LISTING CLOSE;
62 ODS AUTONAVIGATE OFF;
63 ODS GRAPHICS ON;
64 ODS HTML5 (ID=WEB) DEVICE=PNG GPATH="&_SASWSTEMP_" ENCODING=utf8 FILE=_HTMLOUT (TITLE='Results: Occup_Macro_ZM19.sas')
64 ! STYLE=Htmlblue OPTIONS(BITMAP_MODE='INLINE' OUTLINE='ON' SVG_MODE='INLINE'
64 ! CSS_PREFIX='.ods_717c6288-07d7-4390-9c85-69b0718d63f4' BODY_ID='div_717c6288-07d7-4390-9c85-69b0718d63f4' );
65 ODS RTF (ID=WEB) STYLE=Rtf FILE=_RTFOUT sasdate;
66 ODS PDF (ID=WEB) STYLE=Pearl FILE=_PDFOUT;
67 &GRAPHINIT;
68 OPTIONS FIRSTOBS=1;
69 OPTIONS OBS=MAX;
70 OPTIONS DTRESET DATE NUMBER NOTES;
71 OPTIONS NOTES STIMER SOURCE NOSYNTAXCHECK;
72
73
74 /***MACRO*/
75
76 data full_tdata;
77 input id$ 1-6 job 7-8 jobyrin 9-11 jobyrout 18-20 icode$ 23-27 lung$ 29-35;
78 datalines;
79 OSa13373 78 6191 cacase
80 OSa30139 46 7181 cacase
81 OSa30356 64 6191 cacase
82 OSa73123 31 7181 popcon
83 OSa73231 42 5130 popcon
84 OSa86346 60 6198 cacon
85 OSa86460 70 5130 cacon
86 OSa93368 72 6121 popcon
87 OSf26154 54 6198 popcon
88 OSf26270 70 6191 popcon
89 OSh77163 66 6121 cacon
90 OSh77266 70 6121 cacon
91 OSh77370 71 6121 cacon
92 OSi84167 75 6191 cacase
93 OSi84275 81 6191 cacase
94 OSj17360 75 6191 cacase
95 OSj17465 70 6191 cacase
96 ;
97
98 proc print data=full_tdata;
99
100 data small_tdata; set full_tdata; drop lung;
101
102 proc freq data=small_tdata;
103 tables job; run;
104
105 proc print data=small_tdata;
106 run;
107
108
109 /* MACRO TEST*/
110
111 *max 4 jobs per subject;
112
113
114 %macro duration(database=small_tdata, icode=icode, Job_title="baker");
115
116 *Creating a dataset containing all the jobs with selected icode;
117
118 data &Job_title;
119 set &database;
120 if icode = "&6191";
121 run;
122
123
124 *transposing dataset so that the YEARIN and YEAROUT of all jobs held by a subject are in the same row;
125
126
127 *YEARIN;
128
129 proc transpose data = &Job_title out = &Job_title._yearin prefix = YEARIN;
130 by ID;
131 var jobyrin;
132 run;
133
134 proc sort data = &Job_title._yearin;
135 by id;
136 run;
137
138 *YEAROUT;
139
140 proc transpose data = &Job_title out = &Job_title._yearout prefix = YEAROUT;
141 by ID;
142 var jobyrout;
143 run;
144
145 proc sort data = &Job_title._yearout;
146 by id;
147 run;
148
149
150
151 data &Job_title._final;
152 merge &Job_title._yearin &Job_title._yearout;
153 by ID;
154
155 array YEARIN (3) YEARIN1 YEARIN2 YEARIN3;
156 array YEAROUT (3) YEAROUT1 YEAROUT2 YEAROUT3;
157
158 array YEARINv2 (3) YEARIN2 YEARIN3 YEARIN4 ;
159 array YEAROUTv2 (3) YEAROUT2 YEAROUT3 YEAROUT4;
160
161 array YEARIN_F (4) YEARIN1 YEARIN2 YEARIN3 YEARIN4;
162 array YEAROUT_F (4) YEAROUT1 YEAROUT2 YEAROUT3 YEAROUT4;
163
164 Array duration (4) Dur1 Dur2 Dur3 Dur4;
165
166
167
168 do i = 1 to 3;
169
170 if yearin(i) ne . and yearinv2(i) ne . and yearinv2(i) le yearout(i) and yearoutv2(i) ge yearout(i) then do; yearinv2(i)
170 ! = yearin(i); yearin(i) = .;yearout(i) = .;end;
171 else if yearin(i) ne . and yearinv2(i) ne . and yearinv2(i) le yearout(i) and yearoutv2(i) lt yearout(i) then do;
171 ! yearinv2(i) = yearin(i);yearoutv2(i) = yearout(i); yearin(i) = .;yearout(i) = .;end;
172
173 end;
174
175 do i = 1 to 4;
176
177 if YEARIN_F(i) = . then YEARIN_F(i) = 0;
178 if YEAROUT_F(i) = . then YEAROUT_F(i) = 0;
179
180 duration(i) = YEAROUT_F(i) - YEARIN_F(i);
181 if YEARIN_F(i) ne 0 and YEAROUT_F(i) = YEARIN_F(i) then duration(i) = 0.5;
182 end;
183
184 &Job_title._dur = Dur1 + Dur2 + Dur3 + Dur4;
185
186 &Job_title._ever = 1;
187
188 keep ID &Job_title._dur &Job_title._ever;
189 run;
190
191 /* Display the macro results*//*????? I am stuck here, please*/
192
193 %duration(database=small_tdata,icode=icode, Job_title=baker);
194
195 proc print data=&Job_title_final;
196 run;
197
198 %MEND duration;
199
200
201 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
202 ODS HTML CLOSE;
203 &GRAPHTERM; ;*';*";*/;RUN;QUIT;
204 QUIT;RUN;
205 ODS HTML5 (ID=WEB) CLOSE;
206
207 ODS RTF (ID=WEB) CLOSE;
208 ODS PDF (ID=WEB) CLOSE;
209 FILENAME _GSFNAME;
210 DATA _NULL_;
211 RUN;
212 OPTIONS NOTES STIMER SOURCE SYNTAXCHECK;
213
 
Please I am expecting the macro to print out the results of all the necessary variables:
id yearin yearout yearin_f yearout_f job_title_dur job_title_ever.......
Thank you.
ak.
 
 



 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You can't run

 

%duration ( )

 

from inside the macro definition, it has to be after the macro definition. Something like this:

 

%macro duration( );
... your code ...
%mend;

%duration( )
--
Paige Miller

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You defined a macro named DURATION.

But you never called it. So it never ran.

PaigeMiller
Diamond | Level 26

You can't run

 

%duration ( )

 

from inside the macro definition, it has to be after the macro definition. Something like this:

 

%macro duration( );
... your code ...
%mend;

%duration( )
--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 779 views
  • 0 likes
  • 3 in conversation