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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

5 REPLIES 5
ballardw
Super User

And how is this automated process supposed to determine the display value when a new value appears in the data?

sahoositaram555
Pyrite | Level 9
Hi ballardw, I'm also curious to know the same. what i have in the currently existing dataset is only the lefthand side values,(0,1,A) .I need to define it based on a condition(which is if is it 0 then it should be "Non-sepstrata", otherwise anything else would have a "-sepstrata" attached to the value.
Something below i've tried and would like to try with a if statement but not been successful yet.
proc format ;
value $new_a %substr(&stratum,1)=%bquote(%substr(&stratum,1)||"-sepstrata")
%substr(&stratum,2)=%bquote(%substr(&stratum,2)||"-sepstrata")
%substr(&stratum,3)=%bquote(%substr(&stratum,3)||"-sepstrata")
run;
Tom
Super User Tom
Super User

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. 

ballardw
Super User

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;
Astounding
PROC Star

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.

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!

How to Concatenate Values

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.

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
  • 5 replies
  • 664 views
  • 2 likes
  • 4 in conversation