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;
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;
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.
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....
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;
Got it to work. Stupid spelling error..
If the question is answered please mark it as solved. If you could also mark some of your previous questions that would be appreciated.
If you are saving your formats into a permanent library then make sure to set the FMTSEARCH option so that SAS can find them.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.