- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello everybody.
Today I faced with an issue which solution seems pretty straightforward, but I couldn't find any correct approach for it.
I have the below data
What I want to do is following.
1 - To output an additional raw (with certain new variables) if my (have) dataset dd2 does not contain observation with DDTESTCD='PRCDTH'
2 - And in case if it contains DDTESTCD='PRCDTH', then I want a new dd3 dataset with 0 observation,
Thank you in advance
Kind Regards
Artur
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can do this via a macro variable which I have named &FLAG ... something like this outline of the code
%let flag=0;
data _null_;
set dd2;
if ddtestcd='PRCDTH' then do;
call symputx('flag',1);
stop;
end;
run;
%if &flag=1 %then %do;
/* Create data set with zero rows */
data dd3;
set dd2(obs=0);
run;
%end;
%else %do;
/* Some other data step or PROC code, I'm not sure what you want here */
%end;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for your answer @PaigeMiller
I applied the code to my program, but I see some issues. Hope you can help me to figure it out.
%macro prcdth();
%let flagone=0; %let flagtwo=0;
data _null_;
set dd2;
if ddtestcd='PRCDTH' then do;
call symputx('flagone',1);
stop;
end;
run;
data _null_;
set dd2;
if DDTESTCD="CANDUSO" and DDORRES="UNEQUIVOCALLY DUE TO DISEASE UNDER STUDY" then do;
call symputx('flagtwo',1);
stop;
end;
run;
%if &flagone.=1 %then %do; /* Create data set with zero rows */
data dd3_1;
set dd2(obs=0);
run;
%end;
%else %if &flagtwo.=1 %then %do;
data dd3_2;
set dd2;
AVALC = "CANCER";
aval=.;
PARAMCD="PRCDTH";
AVALCAT1='NON-CARDIOVASCULAR CAUSE';
if AVALCAT1='CARDIOVASCULAR CAUSE' then AVALCA1N=1;
else if AVALCAT1='NON-CARDIOVASCULAR CAUSE' then AVALCA1N=2;
output;
run;
%end;
%else %if &flagone.^=1 and &flagtwo.^=1 %then %do;
data dd3_3;
set dd2;
if DTHFL='Y' then do;
AVALC="Unknown";
aval=.;
PARAMCD="PRCDTH";
AVALCAT1=DDSCAT;
if AVALCAT1='CARDIOVASCULAR CAUSE' then AVALCA1N=1;
else if AVALCAT1='NON-CARDIOVASCULAR CAUSE' then AVALCA1N=2;
output;
end;
run;
%end;
%mend prcdth;
%prcdth();
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
61
62
63 %macro prcdth();
64 %let flagone=0; %let flagtwo=0;
65 data _null_;
66 set dd2;
67 if ddtestcd='PRCDTH' then do;
68 call symputx('flagone',1);
69 stop;
70 end;
71 run;
72 data _null_;
73 set dd2;
74 if DDTESTCD="CANDUSO" and DDORRES="UNEQUIVOCALLY DUE TO DISEASE UNDER STUDY" then do;
75 call symputx('flagtwo',1);
76 stop;
77 end;
78 run;
79
80 %if &flagone.=1 %then %do; /* Create data set with zero rows */
81 data dd3_1;
82 set dd2(obs=0);
83 run;
84 %end;
85 %else %if &flagtwo.=1 %then %do;
86 data dd3_2;
87 set dd2;
88
89 AVALC = "CANCER";
90
91 aval=.;
92 PARAMCD="PRCDTH";
93 AVALCAT1='NON-CARDIOVASCULAR CAUSE';
94 if AVALCAT1='CARDIOVASCULAR CAUSE' then AVALCA1N=1;
95 else if AVALCAT1='NON-CARDIOVASCULAR CAUSE' then AVALCA1N=2;
96 output;
97 run;
98 %end;
99 %else %if &flagone.^=1 and &flagtwo.^=1 %then %do;
100 data dd3_3;
101 set dd2;
102
103 if DTHFL='Y' then do;
104
105 AVALC="Unknown";
106 aval=.;
107 PARAMCD="PRCDTH";
108 AVALCAT1=DDSCAT;
109 if AVALCAT1='CARDIOVASCULAR CAUSE' then AVALCA1N=1;
110 else if AVALCAT1='NON-CARDIOVASCULAR CAUSE' then AVALCA1N=2;
111 output;
112 end;
113 run;
114 %end;
115 %mend prcdth;
116 %prcdth();
MPRINT(PRCDTH): data _null_;
MPRINT(PRCDTH): set dd2;
MPRINT(PRCDTH): if ddtestcd='PRCDTH' then do;
MPRINT(PRCDTH): call symputx('flagone',1);
MPRINT(PRCDTH): stop;
MPRINT(PRCDTH): end;
MPRINT(PRCDTH): run;
NOTE: There were 15 observations read from the data set WORK.DD2.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
MPRINT(PRCDTH): data _null_;
MPRINT(PRCDTH): set dd2;
MPRINT(PRCDTH): if DDTESTCD="CANDUSO" and DDORRES="UNEQUIVOCALLY DUE TO DISEASE UNDER STUDY" then do;
MPRINT(PRCDTH): call symputx('flagtwo',1);
MPRINT(PRCDTH): stop;
MPRINT(PRCDTH): end;
MPRINT(PRCDTH): run;
NOTE: There were 42 observations read from the data set WORK.DD2.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
MPRINT(PRCDTH): data dd3_1;
MPRINT(PRCDTH): set dd2(obs=0);
MPRINT(PRCDTH): run;
NOTE: There were 0 observations read from the data set WORK.DD2.
NOTE: The data set WORK.DD3_1 has 0 observations and 47 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
117
118 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
131
So I have added two additional derivations and &flagtwo macro variable.
The second derivation (dd3_2 dataset) I need to execute when the &flagtwo is 1 and after the &flagone else condition (i.e there is no ddtestcd='PRCDTH' in dd2 dataset, but there is an observation satisfying DDTESTCD="CANDUSO" and DDORRES="UNEQUIVOCALLY DUE TO DISEASE UNDER STUDY" condition).
The third derivation (dd3_3 dataset) I need to execute when the &flagone is ^=1 and the &flagtwo is ^=1, after the &flagtwo else condition (i.e. there is no ddtestcd='PRCDTH' in dd2 dataset and also there is no observation satisfying DDTESTCD="CANDUSO" and DDORRES="UNEQUIVOCALLY DUE TO DISEASE UNDER STUDY" condition, but there is an observation satisfying DTHFL='Y' condition).
Before %prcdth macro execution the input dataset dd2 has 1170 observations(checked manually).
But during the macro execution it takes only 15 and 42 observations, and dd3_2 and dd3_3 datasets are not created.
Could you please take a look and give some advice on what need to be adjusted, I have attached the log as well.
Thank you in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
But during the macro execution it takes only 15 and 42 observations
The STOP command makes the DATA step stop processing observations, so the first time it stops at 15 and the next time it stops at 42. This is what I expect.
and dd3_2 and dd3_3 datasets are not created.
That's because you wrote
%else %if &flagtwo.=1 %then %do;
Take the %else out of this line. And remove %else later as well.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Take the %else out of this line. And remove %else later as well
All these three conditions - %if &flagone.=1, %if &flagtwo.=1 and %if &flagone.^=1 and &flagtwo.^=1 are mutually exclusive but dependent from each other. If I remove the %else, then they will be executed independently from each other correct?
second(&flagtwo) condition should be executed when the &flagone. condition is false, and the third condition should be executed when the first two conditions are not met.
So at least(and only) one of them should be executed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
All these three conditions - %if &flagone.=1, %if &flagtwo.=1 and %if &flagone.^=1 and &flagtwo.^=1 are mutually exclusive but dependent from each other. If I remove the %else, then they will be executed independently from each other correct?
Please try it and find out for yourself.
second(&flagtwo) condition should be executed when the &flagone. condition is false, and the third condition should be executed when the first two conditions are not met. So at least(and only) one of them should be executed.
Somewhere, there is a contradiction between your explanation above, and your statement that you want data sets dd3_2 and dd3_3 output. So I don't understand what you want and cannot help until I understand. But in any case, this is a simple matter of coming up with the proper %IF %THEN %ELSE or %IF %THEN %IF %THEN (without %ELSE). Surely, since you know what the desired result is, you can state it clearly in %IF %THEN %ELSE or %IF %THEN %IF %THEN (without %ELSE) language.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you