Hi. In same data A, I would like to count those who answer the number of days to each question for the use of cigarette or marijuana which can range from 0-30 in QN1 and QN2, and code them into any_user . Please note apart from any number from 0 to 3 there are character options to question stem such as 'N' for not answered and 'Z' for skipped.
QN1: During the past 30 days, on how many days did you use e-cigarettes?
N
Z
0
1
2
3
QN2: During the past 30 days, on how many days did you use marijuana?
N
Z
0
1
2
3
Data A, if QN1 in (1-30) or QN2 in (1-30) then any_sub_user=1; else any_sub_user=2; run;
Your syntax for the IN operator is wrong.
data q;
missing N Z;
input qn1 qn2;
if QN1 in (1:30) or QN2 in (1:30)
then any_sub_user=1;
else any_sub_user=0;
cards;
N 3
Z 0
0 N
1 0
2 Z
3 1
;;;;
run;
proc print;
run;
Please run PROC CONTENTS on your data and tell us if these two variables are numeric or character. The variable could be numeric and the Z and N could be coded as missing values .N and .Z.
So really impossible to answer at this time without your PROC CONTENTS results.
proc contents data=have;
run;
If I run PROC CONTENTS on data set SASHELP.CARS, I get this, and you can see that some variables are Num (numeric) and others are Char (character):
With numeric variables, you can't use 1-30 (SAS sees this as subtraction). However, you can use a simple arithmetic test to see if the number is between 1 and 30.
data want;
set have;
if 1<=QN1<=30 or 1<=QN1<=30 then any_sub_user=1;
else any_sub_user=2;
run;
Your syntax for the IN operator is wrong.
data q;
missing N Z;
input qn1 qn2;
if QN1 in (1:30) or QN2 in (1:30)
then any_sub_user=1;
else any_sub_user=0;
cards;
N 3
Z 0
0 N
1 0
2 Z
3 1
;;;;
run;
proc print;
run;
@femiajumobi1 wrote:
Hi. In same data A, I would like to count those who answer the number of days to each question for the use of cigarette or marijuana which can range from 0-30 in QN1 and QN2, and code them into any_user .
(...)
Data A, if QN1 in (1-30) or QN2 in (1-30) then any_sub_user=1; else any_sub_user=2; run;
Alternative solution:
Obviously, you just want to distinguish between positive numbers and zero or missing values in QN1 and QN2. Since negative values do not occur, this amounts to interpreting QN1 and QN2 as Boolean values and the (corrected) IF condition boils down to the Boolean expression QN1 | QN2. Using the definition
any_sub_user = QN1 | QN2;
the sum of variable any_sub_user would yield the desired count (as the numeric values of TRUE and FALSE are 1 and 0, respectively). Similarly, if you really need the value 2 in the "ELSE" case, you can define
any_sub_user = 2-(QN1 | QN2);
Note that the above definitions are still valid if the time span is changed to fewer or more than 30 days and also if non-integer responses (such as 1.5) are accepted.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.