DATA Step, Macro, Functions and more

How to make a data-set mixture of Type = 'C' and Type = 'N' and use it as formats.

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

How to make a data-set mixture of Type = 'C' and Type = 'N' and use it as formats.

 

 

Hi Everyone,

 

Here my concern is how to make a data-set mixture of Type = 'C' and Type = 'N' and use it as formats.

Below are the codes for Character formats and I want a dataset mixture of char and numeric formats.

Please modify the below dataset and codes accordingly. (If it is not possible with below program / codes then please give me some alternate program / codes for the same problem)

 

libname learn '/folders/myfolders/sas';

*-----------------------------------------------------------------------------------*
 Creating a test data set that will be used to make a CNTLIN data set
*-----------------------------------------------------------------------------------*;
data learn.codes;
	input ICD9 : $5. Description & $21.;
datalines;
020 Plague
022 Anthrax
390 Rheumatic fever
410 Myocardial infarction
493 Asthma
540 Appendicitis
;

*----------------------------------------------------------------------*
 Creating a CNTLIN data set from an existing SAS data set
*----------------------------------------------------------------------*;
data control;
	set learn.codes(rename= (ICD9 = Start Description = Label));
	retain Fmtname '$ICDFMT' Type 'C';
run;
title "Demonstrating an Input Control Data Set";
proc format cntlin=control fmtlib;
run;

*------------------------------------------------*
 Using the CNTLIN= created data set
*------------------------------------------------*;
data disease;
	input ICD9 : $5. @@;
datalines;
020 410 500 493
;
title "Listing of DISEASE";
proc print data=disease noobs;
	format ICD9 $ICDFMT.;
run;

*-----------------------------------------------------*
 Adding an OTHER category to your format
*-----------------------------------------------------*;
proc delete data=control;
data control;
	set learn.codes(rename= (ICD9 = Start Description = Label)) end = last;
	
	retain Fmtname '$ICDFMT' Type 'C';
	
	if last then do;
		Start = ' ';
		Hlo = 'o';
		Label = 'Not Found';
	end;
run;

proc format cntlin=control fmtlib;
run; /* Note: Remember to run proc format again */

data disease;
	input ICD9 : $5. @@;
datalines;
020 410 500 493
;
title "Listing of DISEASE";
proc print data=disease noobs;
	format ICD9 $ICDFMT.;
run;


*--------------------------------------------------------------------------*
 Updating an existing format using a CNTLOUT=data set option 
*--------------------------------------------------------------------------*;

proc format cntlout=control_out;
	select $ICDFMT;
run;

data new_control;
	length Label $ 21;
	
	set control_out end=Last;
	output;

	if Last then
		do;
			Hlo=' ';
			
			Start='427.5';
			End=Start;
			Label='Cardiac Arrest';
			output;
			
			Start='466';
			End=Start;
			Label='Bronchitis';
			output;
		end;
run;

proc format cntlin=new_control;
	select $ICDFMT;
run;
Regards,
AG_Stats

Accepted Solutions
Solution
‎05-08-2016 02:13 PM
Super User
Posts: 10,466

Re: How to make a data-set mixture of Type = 'C' and Type = 'N' and use it as formats.

I'm not quite clear on your objective. Are you attempting to create numeric and character version of the same values? Or do you have a different range of numbers that want a different format?

 

If you are attempting to create a SINGLE Cntlin data set then when you create format statements you will need to sort the data by the format name before use in proc format.

 

Does this help?

data codes;
	input ICD9 : $5. Description & $21.;
   start = put (ICD9, z3.);
   label = Description;
   FmtName = '$ICDFMt'; 
   type='C';
   output;
   FmtName = 'ICDFMt'; 
   type='N';
   output;
datalines;
020 Plague
022 Anthrax
390 Rheumatic fever
410 Myocardial infarction
493 Asthma
540 Appendicitis
;
run;

proc sort data=codes;
   by FmtName start;
run;

Proc format library=work cntlin=codes;
run;

data test;
   x=20;
   y='022';
run;

proc print data=test;
   format x Icdfmt. y $Icdfmt.;
run;

View solution in original post


All Replies
Solution
‎05-08-2016 02:13 PM
Super User
Posts: 10,466

Re: How to make a data-set mixture of Type = 'C' and Type = 'N' and use it as formats.

I'm not quite clear on your objective. Are you attempting to create numeric and character version of the same values? Or do you have a different range of numbers that want a different format?

 

If you are attempting to create a SINGLE Cntlin data set then when you create format statements you will need to sort the data by the format name before use in proc format.

 

Does this help?

data codes;
	input ICD9 : $5. Description & $21.;
   start = put (ICD9, z3.);
   label = Description;
   FmtName = '$ICDFMt'; 
   type='C';
   output;
   FmtName = 'ICDFMt'; 
   type='N';
   output;
datalines;
020 Plague
022 Anthrax
390 Rheumatic fever
410 Myocardial infarction
493 Asthma
540 Appendicitis
;
run;

proc sort data=codes;
   by FmtName start;
run;

Proc format library=work cntlin=codes;
run;

data test;
   x=20;
   y='022';
run;

proc print data=test;
   format x Icdfmt. y $Icdfmt.;
run;
Contributor
Posts: 65

Re: How to make a data-set mixture of Type = 'C' and Type = 'N' and use it as formats.

[ Edited ]

Thanks for your reply. 

 

What codes should I use if I have a different range of numbers that want a different format.  For this you can consider the following datalines:

 

/* Or you can consider any values / datalines as you like - that give me good exposure. */

 

 

datalines;
020 Plague
022 Anthrax
390 500.50
410 Myocardial infarction
493 200.50
540 Appendicitis
;

 

Regards,
AG_Stats
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 193 views
  • 1 like
  • 2 in conversation