BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
femiajumobi1
Quartz | Level 8

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;
 
1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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;

Capture.PNG

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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):

 

PaigeMiller_0-1706795071831.png

--
Paige Miller
femiajumobi1
Quartz | Level 8
Both QN1 and QN2 are numeric variables

type (Num), Len (8), Format (BEST.)
Astounding
PROC Star
What does PROC CONTENTS tell you about QN1 and QN2? Are they character or are they numeric?
femiajumobi1
Quartz | Level 8
They are numeric variables (proc contents). Please note am recoding with "OR" to capture in any of the QN where a any numeric from 1 to 30 is mentioned
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
data_null__
Jade | Level 19

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;

Capture.PNG

FreelanceReinh
Jade | Level 19

@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.

femiajumobi1
Quartz | Level 8
This works. Super-efficient codes. Thanks.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 804 views
  • 2 likes
  • 5 in conversation