Hi all,
I wanted use a macro to generate a new variable for multiple data sets with the same data format, but cannot make substr function work in the macro. My original data sets are report1-report3, the data sets to get are report1_ck, report2_ck, and report3_ck. . The last field flag_report_error is the target variable to generate.
data report1; infile datalines truncover dsd; input measure_num Current $ Expect_Report_Qtr $ var1; datalines; 1,2021Q1,Q1,17689 2,2021Q1,Q1, 3,2021Q1,,17036 4,2021Q1,Q2, 5,2021Q1,Q2,
; run; data report2; infile datalines truncover dsd; input measure_num Current $ Expect_Report_Qtr $ var1; datalines; 1,2021Q2,Q1, 2,2021Q2,Q1, 3,2021Q2,,11791 4,2021Q2,Q2,12290 5,2021Q2,Q2,11161 ; run; data report3; infile datalines truncover dsd; input measure_num Current $ Expect_Report_Qtr $ var1; datalines; 1,2021Q3,Q1, 2,2021Q3,Q1, 3,2021Q3,,11207 4,2021Q3,Q2, 5,2021Q3,Q2, ; run;
data report1_ck; infile datalines truncover dsd; input measure_num Current $ Expect_Report_Qtr $ var1 flag_report_error; datalines; 1,2021Q1,Q1,17689,0 2,2021Q1,Q1,,1 3,2021Q1,,17036,0 4,2021Q1,Q2,,0 5,2021Q1,Q2,,0 ; run; data report2_ck; infile datalines truncover dsd; input measure_num Current $ Expect_Report_Qtr $ var1 flag_report_error; datalines; 1,2021Q2,Q1,,0 2,2021Q2,Q1,,0 3,2021Q2,,11791,0 4,2021Q2,Q2,12290,0 5,2021Q2,Q2,11161,0 ; run; data report3_ck; infile datalines truncover dsd; input measure_num Current $ Expect_Report_Qtr $ var1 flag_report_error; datalines; 1,2021Q3,Q1,,0 2,2021Q3,Q1,,0 3,2021Q3,,11207,0 4,2021Q3,Q2,,0 5,2021Q3,Q2,,0 ; run;
The code I use is as follows:
%macro check_field(); %do i=1 %to 3; data report&i._ck; set report&i.; if %sysfunc(substr(Current,5))='Q1' and Expect_Report_Qtr='Q1' and var1=. then flag_report_ERROR=1; /*When the field "current" contains "Q1" and Expect_Report_Qtr has the value of Q1, var1 needs to be non-missing. */ if %sysfunc(substr(Current,5))='Q2' and Expect_Report_Qtr='Q2' and var1=. then flag_report_ERROR=1; /*When the field "current" contains "Q2" and Expect_Report_Qtr has the value of Q2, var1 needs to be non-missing. */ run; proc freq data=report&i._ck; table flag_report_ERROR; run; %end; %mend;
%check_field;
Can anyone help correct my code?
Thank you!
... View more