BookmarkSubscribeRSS Feed
hiteshchauhan1
Obsidian | Level 7

i want to achieve something that i have not tried before.

 

I have a dataset which i am using to make a format using proc format cntlin and data statement.

The issue is that the value which is assigned to the Label variable is what gets mapped on the basis of a defined key. And i have three values to map. I have 2 choices to do the same: 

 

i) Either i make 3 different formats using the above mentioned methods because i have 3 values to map using the same key.

ii) If there is a way that i can have multiple labels and using some criteria i can tell SAS that if the key matches then assign this label value to some other variable.

 

is method ii) possible?

If yes, can please anyone explain that to me. Examples will be very much appriciated.

 

Thanks,

4 REPLIES 4
Tom
Super User Tom
Super User

Create a simple example of your multi-level format using PROC FORMAT code.  Then use the CNTLOUT= option on PROC FORMAT to convert it to a dataset.  Look at that generated dataset and figure out how the multi-level values are stored/flagged.  Then figure out how to recreate that from your source data. Key variables are FMTNAME, START, END, HLO.

Reeza
Super User

You can use multilabel formats. 

This creates a format and then uses CNTLOUT to display it so you can see how you need to structure your input data set. 

 

/*This program shows how an multilabel format works and that the summary statistics can be done at a unique level but also aggregated at a higher level included in the data.*/

/*sample data*/
data have;
input Year    Area $    Profit;
cards;
2001    A   1
2002    A   2
2001    B   1
2001    C   3
2002    C   1
2001    E   4
2002    E   2
2001    F   3
2002    F   4
;;;;
run;

*format - groups A/B/C into D and E/F into G as well;
proc format;
value $ area_fmt (multilabel)
'A' = 'A'
'B' = 'B'
'C' = 'C'
'A', 'B', 'C' = 'D'
'E' = 'E'
'F' = 'F'
'E', 'F' = 'G';
run;

*summary statistics - not CLASS and FORMAT statements;
proc means data=have noprint nway;
class year area / mlf;
format area $area_fmt.;
var profit;
output out=want sum(profit)=profit;
run;



proc format cntlout=demo;
run;

title 'Demo of control data set for a format';
proc print data=demo;
var fmtname start start end label hlo mult sexcl eexcl;
run; 
run;

Results:

 

Demo of control data set for a format

 
Obs FMTNAME START START END LABEL HLO MULT SEXCL EEXCL
1 AREA_FMT A A A A M 0 N N
2 AREA_FMT A A A D M 0 N N
3 AREA_FMT B B B B M 0 N N
4 AREA_FMT B B B D M 0 N N
5 AREA_FMT C C C C M 0 N N
6 AREA_FMT C C C D M 0 N N
7 AREA_FMT E E E E M 0 N N
8 AREA_FMT E E E G M 0 N N
9 AREA_FMT F F F F M 0 N N
10 AREA_FMT F F F G M 0 N N

 


@hiteshchauhan1 wrote:

i want to achieve something that i have not tried before.

 

I have a dataset which i am using to make a format using proc format cntlin and data statement.

The issue is that the value which is assigned to the Label variable is what gets mapped on the basis of a defined key. And i have three values to map. I have 2 choices to do the same: 

 

i) Either i make 3 different formats using the above mentioned methods because i have 3 values to map using the same key.

ii) If there is a way that i can have multiple labels and using some criteria i can tell SAS that if the key matches then assign this label value to some other variable.

 

is method ii) possible?

If yes, can please anyone explain that to me. Examples will be very much appriciated.

 

Thanks,


 

ballardw
Super User

Concrete examples of what you are attempting, start data and expected result, are always a good idea.

You say: "The issue is that the value which is assigned to the Label variable is what gets mapped on the basis of a defined key. And i have three values to map. "

I do not see anything from that text why you might need 3 formats. An explicit example of the mapping might clarify things.

 

ii) If there is a way that i can have multiple labels and using some criteria i can tell SAS that if the key matches then assign this label value to some other variable.

 

You can always assign a formatted value with something like:

 

othervar = put(somevar, fmtname.);

So if this needs to be conditional on a value then:

 

If <condition> then othervar = put(somevar, fmtname.);

 

If you have tried something like this and the results were not what you expect then show: What you attempted, what the starting data was and the expected result.

 

The multilabel formats may work but the multiple nature is only completely honored by a few procedures. The Put function in a data step is not one of them.

 

 

data_null__
Jade | Level 19

@hiteshchauhan1 wrote:

i want to achieve something that i have not tried before.

 

I have a dataset which i am using to make a format using proc format cntlin and data statement.

The issue is that the value which is assigned to the Label variable is what gets mapped on the basis of a defined key. And i have three values to map. I have 2 choices to do the same: 

 

i) Either i make 3 different formats using the above mentioned methods because i have 3 values to map using the same key.

ii) If there is a way that i can have multiple labels and using some criteria i can tell SAS that if the key matches then assign this label value to some other variable.

 

is method ii) possible?

If yes, can please anyone explain that to me. Examples will be very much appriciated.

 

Thanks,


 

 

You can use multi-label formats expand your data as in this example.

 

/*sample data*/
data have;
   input Year Area :$1. Profit;
   cards;
   2001    A   1
   2002    A   2
   2001    B   1
   2001    C   3
   2002    C   1
   2001    E   4
   2002    E   2
   2001    F   3
   2002    F   4
   ;;;;
   run;
proc format;
value $area(notsorted multilabel)
   'A','B','C' = 'A,B,C'
   'A'='A'    'B'='B'   'C'='C'   'E'='E'   'F'='F'
   'E','F'='E,F'
   'A'-'Z'= 'All'
   ;
quit;

/* expand the data */
data haveV / view=haveV;
   _obs_ + 1;
   retain area;
   retain _s1_ ' ';
   set have;
   retain _s2_ ' ';
   run;
proc print;
   run;
proc summary data=haveV nway;
   by _obs_;
   class area / mlf order=data preloadfmt;
   format area $area.;
   output out=expanded(drop=_type_ drop=_s:) idgroup(out(_s1_--_s2_)=);
   run;
proc print;
   run;

 

Have with by variable and makersHave with by variable and makersExpandedExpanded

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1454 views
  • 3 likes
  • 5 in conversation