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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Could you provide some sample data and expected output for us to verify your code/logic?

 

You also need to assign a length to the response_cat variable ahead of time. That alone may resolve your issues.

 

data HSCT_response_cat;
set HSCT_response;
length response_cat $20.;
response_cat = "";
do i = 1 to 8;
if Q71 = i then response_cat = scan("&responses.", i, '|');
end;
run;

View solution in original post

3 REPLIES 3
Reeza
Super User

Could you provide some sample data and expected output for us to verify your code/logic?

 

You also need to assign a length to the response_cat variable ahead of time. That alone may resolve your issues.

 

data HSCT_response_cat;
set HSCT_response;
length response_cat $20.;
response_cat = "";
do i = 1 to 8;
if Q71 = i then response_cat = scan("&responses.", i, '|');
end;
run;
ap1994
Calcite | Level 5
This worked great - thank you!
ballardw
Super User

@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.

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
  • 3 replies
  • 940 views
  • 4 likes
  • 3 in conversation