Hello,
I'm learning to create formats for a bunch of datasets such that if the column value (ID in this case) when converted into that format exists, then I delete that observation.
For eg. My sample dataset:
ID | Indicator | col2 | col3 |
330 | Y | any random value | any random value |
456 | Y | any random value | any random value |
356 | N | any random value | any random value |
689 | Y | any random value | any random value |
there are a bunch of other datasets, and i need to create formats for all of them, so I decided to create a macro for creating formats.
%macro fmt(var,ds);
data &var.;
set &ds.;
fmtname="$&var.";
type = 'c';
start = strip (id);
end = strip (id);
label = "YES";
keep = fmtname type start end label;
run;
proc format cntlin=&var.;
run;
%mend fmt;
%fmt(abc_out, abc_in); /*bunch of more macro calls like this to create formats*/
this is how I plan to use those formats:
data final;
set....;
if put(id,$abc_out) = "YES" then delete;
/*bunch of more if conditions for other formats*/
run;
When I run the format macro, I get a bunch of errors, not sure how to solve those. Appreciate the help!
Logs:
MLOGIC(FMT): Beginning execution.
MLOGIC(FMT): Parameter VAR has value abc_out
MLOGIC(FMT): Parameter DS has value abc_in
SYMBOLGEN: Macro variable VAR resolves to abc_out
SYMBOLGEN: Macro variable DS resolves to abc_in
SYMBOLGEN: Macro variable VAR resolves to abc_out
388: LINE and COLUMN cannot be determined.
NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 388-185: Expecting an arithmetic operator.
76: LINE and COLUMN cannot be determined.
NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 76-322: Syntax error, statement will be ignored.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.ABC_OUT
may be incomplete. When this step was stopped there were 0 observations and 4 variables.
Macro variables will not be resolved inside single quotes. Use double quotes, as in your initial post.
I suspect you have a CARDS statement inside a SAS macro. This is not allowed in SAS so you must move that DATA step outside your macro or read your data in from an external file.
Thank you for your reply, this is the code I'm running:
data dat1;
input id acc apps $;
cards;
1 1 11
2 2 22
2 3 33
1 4 33
2 6 66
2 7 22
;
run;
%macro fmt(var,ds);
data &var.;
set &ds.;
fmtname="$&var.";
type = 'c';
start = strip (id);
end = strip (id);
label = "YES";
keep = fmtname type start end label;
run;
proc format cntlin=&var.;
run;
%mend fmt;
%fmt(dat1_fmt, dat1);
So does the new version work? If not post your SAS log.
This is invalid syntax:
keep = fmtname type start end label;
The equals sign is invalid in a keep statement. Equal signs are necessary for the keep dataset option.
You made the mistake of not testing your SAS code before putting it into a macro. Rule #1 for macro development: start with working macro-free code.
Thank you for your suggestion. I ran the code without the macro and this is what I have as the code that works:
proc sort data=abc nodupkey;
by id;
run;
data abc_format;
set abc;
fmtname = '$state_frmt';
type = 'c';
start = strip(id);
end = strip(id);
label = 'yes';
keep fmtname type start end label;
run;
proc format cntlin= abc_format;
run;
Now when I try and add a macro, I get an error because I'm trying to create a fmtname that has a macro variable and I'm unable to figure out the right way of doing it. would appreciate your inputs 🙂
Below is the code with the macro:
%macro create_format(ds);
proc sort data=&ds. nodupkey;
by id;
run;
data &ds._format;
set &ds.;
fmtname = '$&ds._format';
type = 'c';
start = strip(id);
end = strip(id);
label = 'YES';
keep fmtname type start end label;
run;
proc format cntlin= &ds._format;
run;
%mend;
%create_format(abc);
the error that I get is:
ERROR: Format name '$&DS._FORMAT' is invalid. Observation ignored.
Macro variables will not be resolved inside single quotes. Use double quotes, as in your initial post.
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.