DATA Step, Macro, Functions and more

How to put obs in a range and merge

Reply
Regular Learner
Posts: 1

How to put obs in a range and merge

Hello, I am a beginner SAS 9.4 user and I am trying to do a project for a study. In this study, I have two variables "Kids yelled at me" and "kids called me names" that constitute together to become bullying. The question was also answered on a scale of 0 to 4, o being no bullying and 1-4 being bullying so I would like first like to assign new categories in each variable for those ranges and after that merge with the other variable.

 

ID     Kids yelled at me    Kids called me names

1              0                              0

2              3                              3

3              1                              1

4              4                              4

5              2                               2

6              1                               2

 

 

so I would like in each category to keep 0 as no bullying and all the rest as bullying and after that merge both variables to get total bullying.

 

data want;
set work.main1;
if kids yelled at me=0 then group=No
if kids called me names=0 then group=No
if kids yelled at me=1 2 3 4 then group=yes;
if kids called me names=1 2 3 4 then group=yes;
run;

proc print data=want;
run;

 This is where I am at, it is probably not correct 

Super User
Posts: 23,321

Re: How to put obs in a range and merge

Posted in reply to diversitygal

Your code is really close actually. The fixes required include:

 

1. Change to using actual variable names rather than 'kids yelled at me'. 

2. For checking for multiple values use IN, ie if var1 in (1, 2, 3, 4) 

3. When assigning a value for a character variable it needs to be in quotes, ie group = "No";

4. Semicolons are consistent

5. You likely want IF/ELSE IF, not a bunch of IF statements. 

 

If you still get errors post back. Make sure to include your code and log.

Super User
Posts: 13,338

Re: How to put obs in a range and merge

Posted in reply to diversitygal

There are a number of reasons that a Yes/No variable is often coded numerically as 1 or 0 for some types of analysis.

The below code does that and shows creating a custom format to display text Yes/No.

data have;
   input ID   Ky Kc;
   label
      Ky ='Kids yelled at me'
      KC ='Kids called me names'
   ;
datalines;
1     0    0
2     3    3
3     1    1
4     4    4
5     2    2
6     1    2
;
run;

Proc format library=work;
Value GroupYN
0="No"
1="Yes";
;
run;
data want;
   set have;
   Group = (max(ky,kc)>0);
   format Group GroupYN.; 
run;
Ask a Question
Discussion stats
  • 2 replies
  • 96 views
  • 2 likes
  • 3 in conversation