BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
marianhabesland
Calcite | Level 5

I have two formats that I want to use for the variables "COCd" and "HypRelDeathInd". The formats do not seem to be working. Does it have something to do with the order of the code or is there another problem? No error messages come up.

 

data HypTabs.NDI (label = "Death Data");
retain SSN DeathDt ICD10 COCd HypRelDeathInd;
set HypTabs.NDI ( rename = ( DeathDt = DeathDt_Char )
);

DeathDt = input(DeathDt_Char, YYMMDDD10.);
format DeathDt YYMMDDD10.;

drop DeathDt_Char;


COCd1= substr(ICD10, 1, 3);
if COCd1 = "I25" then COCd = 1;
else if COCd1 = "I50" then COCd = 2;
else if COCd1 = "I63" then COCd = 3;
else COCd = 4;

if COCd= 1 then HypRelDeathInd = 1;
else if COCd= 2 then HypRelDeathInd = 1;
else if COCd= 3 then HypRelDeathInd = 1;
else if COCd= 4 then HypRelDeathInd = 0;

label SSN = "Social Security Number"
DeathDt = "Death Date"
CODCd = "Cause of Death Code"
HypRelDeathInd = "Hypertension-Related Death";
drop COCd1;
run;

 

procformat
library = HypTabs.HypFormats;

value CODCd
. = 'Unknown'
1 = 'Heart Disease'
2 = 'Heart Failure'
3 = 'Stroke'
4 = 'Other COD'
OTHER = 'Unanticipated Value';


value HypRelDeathInd
. = 'Unknown'
0 = 'No'
1 = 'Yes'
OTHER = 'Unexpected Value';
run;

proc sort;
by SSN;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You don't have a FORMAT statement applying the formats to the variables, or at least I didn't see one. 

 


There's a couple of other issues with your code and things you should do in general:

 

1. Format your code - indents/newlines

2. Comment your code, so you understand it later

3. You're mixing types. You cannot convert a character variable to a numeric variable, you need to create a new variable. 

4. Always use a DATA= in your PROCS so you know explicitly what's being run.

 

This still has bugs so it won't run, but I'll leave it to you to debug.

 

proc format
	library = HypTabs.HypFormats;
value CODCd
	. = 'Unknown'
	1 = 'Heart Disease'
	2 = 'Heart Failure'
	3 = 'Stroke'
	4 = 'Other COD'
	OTHER = 'Unanticipated Value';
value HypRelDeathInd
	. = 'Unknown'
	0 = 'No'
	1 = 'Yes'
	OTHER = 'Unexpected Value';
run;



data HypTabs.NDI (label = "Death Data");
	retain SSN DeathDt ICD10 COCd HypRelDeathInd;
	set HypTabs.NDI ( rename = ( DeathDt = DeathDt_Char )
		);
	DeathDt = input(DeathDt_Char, YYMMDDD10.);
	format DeathDt YYMMDDD10.;
	drop DeathDt_Char;
	COCd1= substr(ICD10, 1, 3);

	if COCd1 = "I25" then
		COCd = 1;
	else if COCd1 = "I50" then
		COCd = 2;
	else if COCd1 = "I63" then
		COCd = 3;
	else COCd = 4;

	if COCd= 1 then
		HypRelDeathInd = 1;
	else if COCd= 2 then
		HypRelDeathInd = 1;
	else if COCd= 3 then
		HypRelDeathInd = 1;
	else if COCd= 4 then
		HypRelDeathInd = 0;
	label SSN = "Social Security Number"
		DeathDt = "Death Date"
		CODCd = "Cause of Death Code"
		HypRelDeathInd = "Hypertension-Related Death";

	format HypRelDeathInd HypRelDeathInd. COCd CODCd.;
	drop COCd1;
run;

proc sort data=XXXXXXXXX;
	by SSN;
run;

View solution in original post

6 REPLIES 6
Reeza
Super User

Yes, your formats have to be defined before the data step/where they're used. I personally like to recommend keeping all formats in a separate program all together and use %INCLUDE at the beginning of the program.

marianhabesland
Calcite | Level 5

Ok, that makes sense. However I tried doing it like this and it still did not work.

 

proc format
library = HypTabs.HypFormats;

value CODCd
. = 'Unknown'
1 = 'Heart Disease'
2 = 'Heart Failure'
3 = 'Stroke'
4 = 'Other COD'
OTHER = 'Unanticipated Value';

value HypRelDeathInd
. = 'Unknown'
0 = 'No'
1 = 'Yes'
OTHER = 'Unexpected Value';
run;

 

data HypTabs.NDI (LABEL = "Death Data");
retain SSN DeathDt ICD10 COCd HypRelDeathInd;
set HypTabs.NDI ( RENAME = ( DeathDt = DeathDt_Char )
);

 

Rest of code....

Reeza
Super User

You don't have a FORMAT statement applying the formats to the variables, or at least I didn't see one. 

 


There's a couple of other issues with your code and things you should do in general:

 

1. Format your code - indents/newlines

2. Comment your code, so you understand it later

3. You're mixing types. You cannot convert a character variable to a numeric variable, you need to create a new variable. 

4. Always use a DATA= in your PROCS so you know explicitly what's being run.

 

This still has bugs so it won't run, but I'll leave it to you to debug.

 

proc format
	library = HypTabs.HypFormats;
value CODCd
	. = 'Unknown'
	1 = 'Heart Disease'
	2 = 'Heart Failure'
	3 = 'Stroke'
	4 = 'Other COD'
	OTHER = 'Unanticipated Value';
value HypRelDeathInd
	. = 'Unknown'
	0 = 'No'
	1 = 'Yes'
	OTHER = 'Unexpected Value';
run;



data HypTabs.NDI (label = "Death Data");
	retain SSN DeathDt ICD10 COCd HypRelDeathInd;
	set HypTabs.NDI ( rename = ( DeathDt = DeathDt_Char )
		);
	DeathDt = input(DeathDt_Char, YYMMDDD10.);
	format DeathDt YYMMDDD10.;
	drop DeathDt_Char;
	COCd1= substr(ICD10, 1, 3);

	if COCd1 = "I25" then
		COCd = 1;
	else if COCd1 = "I50" then
		COCd = 2;
	else if COCd1 = "I63" then
		COCd = 3;
	else COCd = 4;

	if COCd= 1 then
		HypRelDeathInd = 1;
	else if COCd= 2 then
		HypRelDeathInd = 1;
	else if COCd= 3 then
		HypRelDeathInd = 1;
	else if COCd= 4 then
		HypRelDeathInd = 0;
	label SSN = "Social Security Number"
		DeathDt = "Death Date"
		CODCd = "Cause of Death Code"
		HypRelDeathInd = "Hypertension-Related Death";

	format HypRelDeathInd HypRelDeathInd. COCd CODCd.;
	drop COCd1;
run;

proc sort data=XXXXXXXXX;
	by SSN;
run;
marianhabesland
Calcite | Level 5

Got it to work. Stupid spelling error..

Reeza
Super User

If the question is answered please mark it as solved. If you could also mark some of your previous questions that would be appreciated. 

 

Tom
Super User Tom
Super User

If you are saving your formats into a permanent library then make sure to set the FMTSEARCH option so that SAS can find them.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Develop Code with SAS Studio

Get started using SAS Studio to write, run and debug your SAS programs.

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
  • 6 replies
  • 3958 views
  • 0 likes
  • 3 in conversation