- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I have some data and want to create a new variable from my current variables. I basically want the following outcome:
IF var1 = 0 and var2 = 0 then newvariable = 1
If var1 =0 and var2 = 1 then newvariable=2
If var1 = 1 and var2=0 then newvariable = 3
if var1 = 1 and var2=1 then newvariable = 4
I have tried the following and it has not worked
Data new;
set old;
IF var1 = 0 and var2 = 0 then newvariable = 1;
else If var1 =0 and var2 = 1 then newvariable=2;
else If var1 = 1 and var2=0 then newvariable = 3;
else if var1 = 1 and var2=1 then newvariable = 4;
RUN;
Any help would be greatly appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What is the input?
What is the output?
Your if statements look ok.
FYI all you need is a single assignment instead of 4 ifs:
newvariable = 2 * var1 + var2 + 1;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I've attached an example of my input (i've tried with and without parentheses)
When I print the new variable "group8", I would expect to see it coded 1,2,3 or 4 for each subject, but it is just blank
At the end of this I basically want a new variable "group8" coded so I have 4 levels based on those original variables:
treatment 1 + diagnosis 1 = 1
treatment 2 + diagnosis 1 = 2
treatment 1 + diagnosis 2 = 3
treatment 2 + diagnosis 2 = 4
Hope this makes sense. Thank you for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What is the input? Is group8 a character variable in the input data set? You need to provide more info.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
No, group8 is an arbitrary variable name I am trying to create based on these diagnosis and treatment variables. It does not exist in the input data set.
The input for the other variables is something like this -
What I want is SAS to code my data based on these variables so it puts out something like this:
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
No one is going to be able to help you if you show "input something like this." Show the actual input. Show the actual output. Show the actual code. If the data set is too big or proprietary, make up a small sample proof of concept data set. Showing proc contents results for the input would help too. Show the SAS log. Are there notes, errors, or warnings? Solving problems like this is simple only if you examine all the information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Here is a sample data set and solution. It works for me. Try it out:
data run1;
input subject treatment diagnosis;
datalines;
1 0 1
2 1 0
3 0 0
4 0 1
5 1 0
6 1 1
7 0 1
8 1 0
9 0 0
;
data newrun1;
set run1;
if treatment=0 and diagnosis=0 then group8=1;
else if treatment=0 and diagnosis=1 then group8=2;
else if treatment=1 and diagnosis=0 then group8=3;
else if treatment=1 and diagnosis=1 then group8=4;
run;
proc print; run;
Most likely, the problem is your data. Either your input data set doesn't have the variables Treatment and Diagnosis (check your spelling!) or those variables do not have the values you think they do. Use the following to examine the values of your variables:
proc freq data=run1;
tables treatment * diagnosis / norow nocol nopercent;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Rose1 wrote:
...
When I print the new variable "group8", I would expect to see it coded 1,2,3 or 4 for each subject, but it is just blank
Check the log! Are there any unexpected notes, warnings?