BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

I want to create format from data set.

Then I want to use this format in order to create character variable with formatted values.

My question-

What is the way that all values that are not in the format data set will get value "Other"?

So I want that values 100 and 80 will get value "Other" in X_CAT column

Ronein_0-1700735735570.png

 

 

 

/* How Do I Build a Format from a Dataset*/
/* How Do I Build a Format from a Dataset*/
/* How Do I Build a Format from a Dataset*/
/******Example1*******/
/******Example1*******/
/******Example1*******/
data Format_Tbl;
input Code : $char32.  Code_name : $char11. ;
cards;
101 Aberdeen
102 Altrincham
103 Ashford
104 Barnsley
105 Basildon
106 Basingstoke
107 BathFirst
  ;
Run;

data work.outfmt(keep=start label fmtname);  
set work.Format_Tbl(rename=(Code=start Code_name=label));  
fmtname='outfmt';
run;

proc format library=work cntlin=work.outfmt;
run;
/**In  LOG we can see-NOTE: Format OUTFMT has been output.****/


/***Now we can use the format we created***/
/***Now we can use the format we created***/
/***Now we can use the format we created***/
Data tbl;
input x;
cards;
101
106
104
103
100
80
;
Run;

Data want;
set tbl;
X_CAT=PUT(x,outfmt.);
Run;

 
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data Format_Tbl;
input Code : $char32.  Code_name : $char11. ;
cards;
101 Aberdeen
102 Altrincham
103 Ashford
104 Barnsley
105 Basildon
106 Basingstoke
107 BathFirst
  ;
Run;

data work.outfmt(keep=start label fmtname hlo);  
set work.Format_Tbl(rename=(Code=start Code_name=label)) end=last;  
fmtname='outfmt';type='n';
output;
if last then do;start=' ';hlo='o';label='other';output;end;
run;

proc format library=work cntlin=work.outfmt;
run;
/**In  LOG we can see-NOTE: Format OUTFMT has been output.****/


/***Now we can use the format we created***/
/***Now we can use the format we created***/
/***Now we can use the format we created***/
Data tbl;
input x;
cards;
101
106
104
103
100
80
;
Run;

Data want;
set tbl;
X_CAT=PUT(x,outfmt.);
Run;

Ksharp_0-1700738664606.png

 

View solution in original post

2 REPLIES 2
LinusH
Tourmaline | Level 20

You can use a variable named HLO and set it to O for the "other" observation in your CNTLIN data set.

SAS Help Center: Creating a Format from a CNTLIN= Data Set

Data never sleeps
Ksharp
Super User
data Format_Tbl;
input Code : $char32.  Code_name : $char11. ;
cards;
101 Aberdeen
102 Altrincham
103 Ashford
104 Barnsley
105 Basildon
106 Basingstoke
107 BathFirst
  ;
Run;

data work.outfmt(keep=start label fmtname hlo);  
set work.Format_Tbl(rename=(Code=start Code_name=label)) end=last;  
fmtname='outfmt';type='n';
output;
if last then do;start=' ';hlo='o';label='other';output;end;
run;

proc format library=work cntlin=work.outfmt;
run;
/**In  LOG we can see-NOTE: Format OUTFMT has been output.****/


/***Now we can use the format we created***/
/***Now we can use the format we created***/
/***Now we can use the format we created***/
Data tbl;
input x;
cards;
101
106
104
103
100
80
;
Run;

Data want;
set tbl;
X_CAT=PUT(x,outfmt.);
Run;

Ksharp_0-1700738664606.png

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 940 views
  • 1 like
  • 3 in conversation