BookmarkSubscribeRSS Feed
diversitygal
Calcite | Level 5

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 

2 REPLIES 2
Reeza
Super User

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.

ballardw
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 669 views
  • 2 likes
  • 3 in conversation