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
/* 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;
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;
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 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;
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.
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.