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-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
  • 2 replies
  • 551 views
  • 1 like
  • 3 in conversation