- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello all,
I have a question in regards to using an array to recode what a variable codes for.
I have 10 variables (Q1-Q10). I have a proc format making it so 1="No" and 2="Yes" but am instructed to use an array to change 2="Yes" to 0="Yes" does anyone have tips on how to do this?
Should I make an array to say everytime there is a 2 to change it to a 0, and then add that 0="Yes" in the proc format? Or is there a more direct way to do this?
Thank you!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@shortyofhb wrote:
...
But how would I use the array to recode my 2 to 0 while still formatting out as "Yes" in the output?
You don't, those are two different things. So you need to take two different actions to make them both happen. Recoding the variable is a change the values stored in the variable. Similar to your change of the values from 9 to missing.
But changing what is printed for that value is making a change to the format that is attached to the variable. For that part you have two choices. Make a new format with the same name as was used before and leave the same format attached to the variables. Or you could make a new format with a NEW name and attach that format to the variables. The second probably preferred as it will cause less confusion.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@shortyofhb wrote:
Hello all,
I have a question in regards to using an array to recode what a variable codes for.
I have 10 variables (Q1-Q10). I have a proc format making it so 1="No" and 2="Yes" but am instructed to use an array to change 2="Yes" to 0="Yes" does anyone have tips on how to do this?
Should I make an array to say everytime there is a 2 to change it to a 0, and then add that 0="Yes" in the proc format? Or is there a more direct way to do this?
Thank you!
Sounds like a plan. The array is likely the shortest code to change to change the values.
The format would have to be changed if you intend to use it with the recoded values.
My personal approach would be an INFORMAT used to read the data as needed but that may be jumping ahead of an instructor.
I am going to guess that the reason is so "Yes" sorts before "No" at some point.
Personally Yes=1 and No=0 make more sense because 1) sum of the variable is number of yes responses, 2) mean of the variable is the percent of values entered as "yes" in decimal form: .125 is 12.5%. But perhaps "No" is more important for this project.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
Thank you for the response. I would use an informat statement but the instructions we are learning says to use an array to change the code. Is there a general way to do this, or do you need to see my code?
I am using an external excel dataset that I imported in.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/
One of the examples is pretty close to what you're looking for. Another paper that would be helpful for you to read is "Proc Format: Not Just Another Pretty Face" that lays out formats and how to recode variables with formats.
@shortyofhb wrote:
Hello all,
I have a question in regards to using an array to recode what a variable codes for.
I have 10 variables (Q1-Q10). I have a proc format making it so 1="No" and 2="Yes" but am instructed to use an array to change 2="Yes" to 0="Yes" does anyone have tips on how to do this?
Should I make an array to say everytime there is a 2 to change it to a 0, and then add that 0="Yes" in the proc format? Or is there a more direct way to do this?
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
So I used one array to recode my 9 to "Missing" (.) I just did the array statment then did a Do loop where it says If 9 then .
But how would I use the array to recode my 2 to 0 while still formatting out as "Yes" in the output?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@shortyofhb wrote:
...
But how would I use the array to recode my 2 to 0 while still formatting out as "Yes" in the output?
You don't, those are two different things. So you need to take two different actions to make them both happen. Recoding the variable is a change the values stored in the variable. Similar to your change of the values from 9 to missing.
But changing what is printed for that value is making a change to the format that is attached to the variable. For that part you have two choices. Make a new format with the same name as was used before and leave the same format attached to the variables. Or you could make a new format with a NEW name and attach that format to the variables. The second probably preferred as it will cause less confusion.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Oh okay I think I see what you are saying.
So then I have one array that recodes my 9's to .'s.
I will have a second array that recodes all my 2's to 0's.
I will use a new value statement in my proc format to now tell SAS that 0=Yes right? And then attach this value to the variable I recoded?
Or am I interpreting this wrong? Do I remove the previous format from that was one the variables before this new one?
I currently have a proc format.
My variables
Then a format option to tell SAS what I want the variable to code for.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sounds right. Note that you should be able to recode 9 to missing and 0 to 2 inside the same DO loop that is looping over the same array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@shortyofhb wrote:
Do I remove the previous format from that was one the variables before this new one?
The choice between modifying your existing format or creating another format can be situational.
Consider that the same format can be applied to many variables, as you apparently are doing now.
Suppose another question, Say Q25 is currently using the same format for the 2/1 values but for some reason is not re-coded for some reason. If you replace or modify the existing format to only use 0/1 values then Q25 is now missing a code for its 2 value. So you might either modify your existing format to include both 0 and 2 as "Yes" along with the 1 for "No" to allow continued use of the same format for Q1 - Q10 and Q25. Or you could create a new format that only uses the 0/1 coding for Yes/No.
If you use the approach of coding both 0 and 2 to "Yes" you may get questions about appearance order for different variables though.
Here is a very small example of similar formats applied to data somewhat like what you are discussing.
proc format; value yn 0 ='Yes' 1 ='No' ; value yny 0,2 ='Yes' 1 ='No' ; value ny 2 ='Yes' 1 ='No' ; run; data example; input v1 - v4; datalines; 1 1 1 1 0 2 2 2 1 0 0 1 ; proc freq data=example; tables v1 - v4; format v1 yn. v2 yny. v3 ny. v4 yny.; run;
Note the behavior of the values for V2 and V3 with the same values but different formats. And V2 and V4 with the same format but different values.
So you get to make some choices about new/replaced formats. Depending on actual use there may not be a single correct answer.