@ap1994 wrote:
Version: SAS 9.4 TS Level 1M3
Hi all,
I am trying to take a categorical variable wherein the value of each observation can be 1-8, and each number represents a category, and assign the text string category each numerical value represents into a new variable. The reason I am trying to do this is to assign the category that corresponds to each number as the text expression each number represents in a new variable. I am trying to use a Do loop and the scan() function to so that for each possible value of i in the categorical variable, the new variable will return the string from the responses list at the ith position. My code is below; the issue I am seeing with this code is that the resulting output only provides the 1st character in each string. Is there a way to use a Do loop and macro list that will assign the category to a new variable?
%let responses = CR|PR|RELAPSE_lt12|RELAPSE_gt12|CLEAR| 100_DAY_MORTALITY|POST_TX_COMPLICATION|DEATH|UNKNOWN;
/*Q71 is the variable with values 1-8 that represents one of the categories in &responses.*/ data HSCT_response_cat; set HSCT_response; response_cat = ""; do i = 1 to 8; if Q71 = i then response_cat = scan("&responses.", i, '|'); end; run;
Any help is much appreciated!
What you seem to be doing is the role of a custom format. You can display pretty much any given text for a given value. Without adding any additional variables. Plus if the data adds codes you only have to change the format definition to apply the new value.
Also you can create groups of code values that will be honored by analysis graphing procedures generally.
proc format library=work;
value code
1="CR"
2="PR"
3="RELAPSE_lt12"
4="RELAPSE_gt12"
5="CLEAR"
6="100_DAY_MORTALITY"
7="POST_TX_COMPLICATION"
8="DEATH"
9="UNKNOWN"
;
run;
data example;
do i=1 to 9;
output;
end;
run;
proc print data=example;
var i;
format i code.;
run;
proc format library=work;
value codegrp
1,2 = "CR/PR"
3,4 = "Relapse"
5="CLEAR"
6,8="MORTALITY"
7="POST_TX_COMPLICATION"
9="UNKNOWN"
;
run;
proc freq data=example;
tables i;
format i codegrp.;
run;
If you have a format and really need a text variable then
newvar = put(variablename, formatname.);
The proc format code is often much easier to assign ranges and has a number of other options that are well worth learning.
In some cases if you have a good document explaining your variables and values you may also create formats from a data set.
... View more