BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I am trying to create a new variable out of several other variables. This is the code I have so far:

if v2509 = 1 or v2510 = 1 or v2511 = 1 or v2512 = 1 or v2513 = 1 or v2514 = 1 or v2515 = 1 or v2516 = 1 then program_participation = 1;
if v2509 = 0 or v2510 = 0 or v2511 = 0 or v2512 = 0 or v2513 = 0 or v2514 = 0 or v2515 = 0 or v2516 = 0 then program_participation = 0;
run;

When I run a proc means on this, only the individual variables show up and no program_participation (the new variable i want to make out of the others) shows up.

Is there something I"m missing?

thank you for your time
4 REPLIES 4
Cynthia_sas
SAS Super FREQ
Hi:
Which file are you sending to PROC MEANS?? For example, let's say that you have this scenario:

File A = File without PROGRAM_PARTICIPATION variable

File B = File -with- PROGRAM_PARTICIPATION variable

If you send File A to PROC MEANS, then program_participation will not be available for PROC MEANS to do analysis. If your ENTIRE program is shown below, then there is a disconnect between what you want and what you're doing.

Also, Proc Means does not support an IF statement...so if you were trying to put your IF statement inside PROC MEANS syntax, then you should find that your LOG file has some error messages. A general program flow would be something like this:
[pre]

Data File_B;
set File_A; /* read in File_A to calc new variable */
if v2509 = 1 or v2510 = 1 or v2511 = 1 or v2512 = 1 or v2513 = 1 or v2514 = 1 or v2515 = 1 or v2516 = 1 then program_participation = 1;
if v2509 = 0 or v2510 = 0 or v2511 = 0 or v2512 = 0 or v2513 = 0 or v2514 = 0 or v2515 = 0 or v2516 = 0 then program_participation = 0;
run;

proc freq data=File_B;
tables program_participation;
run;

proc means data=File_B;
class program_participation;
var numvar;
run;

[/pre]

I also wonder about your logic...what about an observation where
v2509 = 1 and v2510 = 0 and v2511 = 0...meaning 1 of the vars is 1 and the rest are 0 .... in that case, would program_participation be 1 or 0?? With your logic, if program_participation is set to 1 in the first IF statement (because v2509=1), if any of the other variables are 0, then program_participation will be reset to 0 with the second IF statement. Is this what you want to have happen?

cynthia
Doc_Duke
Rhodochrosite | Level 12
I agree with Cynthia, it;s the logic. Multiple "or"s are notorious for causing this sort of conundrum.

If ANY of the variables =1 means program participation, then the first statement is correct. If you have NO missing data, you can get the non-participation by changing he OR's to AND's, viz:

if v2509 = 1 or v2510 = 1 or v2511 = 1 or v2512 = 1 or v2513 = 1 or v2514 = 1 or v2515 = 1 or v2516 = 1 then program_participation = 1;
if v2509 = 0 AND v2510 = 0 AND v2511 = 0 AND v2512 = 0 AND v2513 = 0 AND v2514 = 0 AND v2515 = 0 AND v2516 = 0 then program_participation = 0;
run;

However, most of us have to contend with missing data. These sort of variables are often scored as a 1 if any are 1, and a 0 if any are checked and no 1's, and a missing only if all are missing. If you want that scoring, then the MAX function is the key:

program_participation=MAX(OF v2509-v2516);

as it returns a missing only if all elements are missing.

Doc
deleted_user
Not applicable
I believe the condition/output is not very clear in the sample given above. If we define the output, the logical representaion can be created.
deleted_user
Not applicable
if v2509 = 1 or v2510 = 1 or v2511 = 1 or v2512 = 1 or v2513 = 1 or v2514 = 1 or v2515 = 1 or v2516 = 1 then program_participation = 1;
if v2509 = 0 or v2510 = 0 or v2511 = 0 or v2512 = 0 or v2513 = 0 or v2514 = 0 or v2515 = 0 or v2516 = 0 then program_participation = 0;

Your logic is incorrect, I think. Say (only 3),

if v2509 = 1 or v2510 = 1 or v2511 = 1 then program_participation = 1;
if v2509 = 0 or v2510 = 0 or v2511 = 0 then program_participation = 0;

V2509 V2510 V2511 program_participation sum(of V2509-V2511)

0 0 0 0 0
1 0 0 0 1
0 1 0 0 1
0 0 1 0 1
1 1 0 0 2
1 0 1 0 2
0 1 1 0 2
1 1 1 1 3

If this is the case you could write

if sum(of V2509-V2511) = 3 then program_participation = 1;
else program_participation = 0;

But I don't think that is the way you want

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 738 views
  • 0 likes
  • 3 in conversation