Hi,
I've created a user defined format by using proc format statements.Would like to create a macro over it in a way that if the input data changes, the code should able to adjust the change accordingly.
Here is the code:
`proc format ;
value $a 1='1-sepstrata'
0='0-Non-sepstrata'
A='A-sepstrata';
run;
`
In my current dateset I've,a columns named stratum which has unique values such as 1,0,A.
Kindly suggest how to go about solving this.
Presumably you have a way to examine your data set to get the equivalent of all the values that appear, something equivalent to:
data strata;
input stratum $;
datalines;
A
0
1
;
Notice this is data, no macro language involved.
With slight changes to this data, the process can be automated easily:
data want;
set strata;
retain fmtname '$a';
start = stratum;
if stratum='0' then label = '0-Non-sepstrata';
else label = catx('-', stratum, 'sepstrata');
run;
proc format cntlin=want;
run;
This is standard technique for creating a format using a SAS data set as input.
And how is this automated process supposed to determine the display value when a new value appears in the data?
If you have as input a macro variable with a delimited list of strata. Such as
%let strata=0|1|A ;
Then you can use a %DO loop.
proc format ;
value $new_a
%do index=1 %to %sysfunc(countw(&strata,|));
%let stratum=%scan(&strata,&index,|);
%if "&stratum"="0" %then %do;
"&startum"="&stratum-Non-sepstrata"
%end;
%else %do;
"&startum"="&stratum-sepstrata"
%end;
%end;
;
run;
Note that to use %DO loop you need to code this inside of a macro.
Note there is no need for macro quoting, but that you need to use double quotes around the values instead of single quotes so that SAS will evaluate the macro triggers.
It's too bad that your variable isn't numeric. It would be very easy with numeric values:
proc format; picture mypict 0 = '0-Non-sepstrata' (noedit) other ='09-sepstrata' ; run; data _null_; do i=0 to 20; put i= mypict.; end; run;
The log will show different values.
With the values you seem to have investigate the use of Proc FCMP to create a custom function that will create the string value XXX-sepstrata given a string value other than 1. Then the format would look like
proc fcmp outlib=sasuser.funcs.sep; function sepstrata (str $ ) $ ; if str='0' then return('0-Non-sepstrata'); else return(catx('-',str,'sepstrata')); endsub; run; /* make the function available*/ options cmplib=(sasuser.funcs); proc format cntlout=work.ctl; value $new_sep (default=20 ) other = [sepstrata()] ; run; data _null_; length A $ 5; do a='0','1','A','B','Fred'; put a= $new_sep,; end; run;
Presumably you have a way to examine your data set to get the equivalent of all the values that appear, something equivalent to:
data strata;
input stratum $;
datalines;
A
0
1
;
Notice this is data, no macro language involved.
With slight changes to this data, the process can be automated easily:
data want;
set strata;
retain fmtname '$a';
start = stratum;
if stratum='0' then label = '0-Non-sepstrata';
else label = catx('-', stratum, 'sepstrata');
run;
proc format cntlin=want;
run;
This is standard technique for creating a format using a SAS data set as input.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.