BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PhanS
Obsidian | Level 7

Hello all,

 

I posted my codes - i wonder if one of you can advise me for a shorter codes than mine. Please kindly see below. Great appreciate your time.

if food= . or cloth= . or cost_h= . or risk_hl= . then do;
if food=1 or cloth=1 or cost_h=1 or risk_hl=1 then any_1bneed=1;
else if food=0 or cloth=0 or cost_h=0 or risk_hl=0 then any_1bneed=0;
else any_1bneed=.;
end;

if food= . or cloth= . or cost_h= . or risk_hl= . then do;
if      food=1 and cloth=1 and cost_h=0 or risk_hl=0 then any_2bneed=1;
else if cost_h=1 and risk_hl=1 and food=0 or cloth=0 then any_2bneed=2;
else if food=1 and cost_h=1 and cloth=0 or risk_hl=0 then any_2bneed=3;
else if food=1 and risk_hl=1 and cloth=0 or cost_h=0 then any_2bneed=4;
else if cloth=1 and cost_h=1 and food=0 or risk_hl=0 then any_2bneed=5;
else if cloth=1 and risk_hl=1 and food=0 or cost_h=0 then any_2bneed=6;
else any_2bneed= .;
end;

if food= . or cloth= . or cost_h= . or risk_hl= . then do;
if      food=1 and cloth=1 and cost_h=1 and risk_hl=0 then any_3bneed=1;
else if food=1 and cloth=1 and cost_h=0 and risk_hl=1 then any_3bneed=2;
else if food=1 and cloth=0 and cost_h=1 and risk_hl=1 then any_2bneed=3;
else if food=0 and cloth=1 and cost_h=1 and risk_hl=1 then any_2bneed=4;
else any_3bneed= .;
end;

if food= . or cloth= . or cost_h= . or risk_hl= . then do;
if food=1 and cloth=1 and cost_h=1 and risk_hl=1 then all_4bneed=1;
else if food=0 and cloth=0 and cost_h=0 and risk_hl=0 then all_4bneed=0;
else all_4bneed=.;
end;

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

I don't think your code does what you want. If I'm wrong, just ignore this post.

 

However, I think that the following does do what you want. Notice that I've changed your input dataset in order to capture all of the possible combinations. I accounted for this missing ID values while inputting the data.

 

data have (drop=x);
  retain id;
  do time=1 to 4;
    input x FD CL HS HL;
    if not missing(x) then ID=x;
    output; 
  end;
cards; 
1 1 . 0 . 
1 . 1 0 0
. . . 1 . 
1 0 0 0 1 
2 1 1 . .   
2 1 . 1 . 
. 1 . . 1  
2 . 1 1 0
2 . 1 0 1  
2 0 0 1 1  
. . . . .
3 1 1 1 .
3 1 1 . 1 
3 0 1 1 1
. . . . . 
4 1 1 1 1
5 0 . 0 1 
5 0 . 0 0  
. . . . .
5 0 0 . 0  
6 1 1 1 1  
6 1 1 1 1 
. . . . .
6 1 1 1 1
7 1 1 1 . 
7 1 1 0 . 
. . . . . 
7 1 1 0 1 
8 1 0 0 0 
8 0 0 1 1 
. . . . .
8 1 0 . 1
9 1 0 0 .  
9 0 0 0 . 
. . . . .
9 1 . 0 0
10 0 1 . 0 
10 1 1 0 .  
. . . . .  
10 1 1 0 1 
; 
run;

proc format;
  value anytwo
  2='1*2'
  3='1*3'
  4='1*4'
  6='2*3'
  8='2*4'
 12='3*4'
 ;
  value anythree
  6='1*2*3'
  8='1*2*4'
 24='2*3*4'
 ;
run;

data want (drop= i _:);
  set have;
  array items(4) FD CL HS HL;
  array _items(4);
  array needs(4) any_1bneed any_2bneed any_3bneed all_4bneed;
  format any_2bneed anytwo.;
  format any_3bneed anythree.;
  if nmiss(of items(*)) lt 4 then do;
    do i=1 to 4;
      _items(i)=items(i);
      needs(i)=0;
    end;
    if sum(of items(*)) eq 1 then any_1bneed=whichn(1,of items(*));
    else if sum(of items(*)) eq 2 then do;
      any_2bneed=whichn(1,of items(*));
      _items(any_2bneed)=0;
      any_2bneed=any_2bneed*whichn(1,of _items(*));
    end;
    else if sum(of items(*)) eq 3 then do;
      any_3bneed=whichn(1,of items(*));
      _items(any_3bneed)=0;
      any_3bneed=any_3bneed*whichn(1,of _items(*));
      _items(whichn(1,of _items(*)))=0;
      any_3bneed=any_3bneed*whichn(1,of _items(*));
    end;
    else all_4bneed=1;
  end;
run;

 

Art, CEO, AnalystFinder.com

View solution in original post

9 REPLIES 9
Reeza
Super User

Can you make a table of all your cases and what they map to?

If you can build that table, you can join it and it'll be cleaner. 

 

ie

 

food cloth cost_h risk_hl group

 

 

PhanS
Obsidian | Level 7

Hi ie,

 

data have;
input ID @; 
  do time1 to time4;
    input FD CL HS HL @;    
    output; 
  end;
cards; 

1 1 1 1 1 
1 1 . 1 1
. . . . . 
1 1 1 1 1 

2 1 1 1 .   
2 1 1 0 . 
. . . . .  
2 1 1 0 1

3 1 0 0 0  
3 0 0 1 1  
. . . . .
3 1 0 . 1

4 1 0 0 . 
4 0 0 0 .
. . . . . 
4 1 . 0 0

5 0 . 0 0 
5 0 . 0 0  
. . . . .
5 0 0 . 0  

6 1 1 1 1  
6 1 1 1 1 
. . . . .
6 1 1 1 1

7 1 1 1 . 
7 1 1 0 . 
. . . . . 
7 1 1 0 1 
 
8 1 0 0 0 
8 0 0 1 1 
. . . . .
8 1 0 . 1

9 1 0 0 .  
9 0 0 0 . 
. . . . .
9 1 . 0 0

10 0 . 0 0 
10 1 1 0 .  
. . . . .  
10 1 1 0 1 

; 
run;

time 3 do not have data
PGStats
Opal | Level 21

Note that any_3bneed and all_4bneed will always be missing because, for example, it is impossible for both

 

food= . or cloth= . or cost_h= . or risk_hl= .

 

and

 

food=1 and cloth=1 and cost_h=1 and risk_hl=1

to be true at the same time.

PG
art297
Opal | Level 21

My question is similar to the one that @PGStats asked.

 

You never set any_2bneed or any_3bneed to missing, and you never calculate any of the values unless there is at least one missing value. Is that really what you want to do?

 

Art, CEO, AnalystFinder.com

 

PhanS
Obsidian | Level 7

Hello Art,

 

I am looking for:

a) at least one out of the fourth items to be 1 (or yes)

2) all of the fourth items to be 1 (or yes)

3) at least 2 out of the four items to be 1 (yes)

4 at least 3  out of the four items to be 1 (yes)

 

My codes showed the results.

However, I would like to learn if there such SAS codes that can support mine. In the other words, to have the same results as my analyses.

 

Phan S.

 

PhanS
Obsidian | Level 7

Hello PGStats,

 

I am looking for:

a) at least one out of the fourth items to be 1 (or yes)

2) all of the fourth items to be 1 (or yes)

3) at least 2 out of the four items to be 1 (yes)

4 at least 3  out of the four items to be 1 (yes)

 

My codes showed the results.

However, I would like to learn if there such SAS codes that can support mine. In the other words, to have the same results as my analyses.

 

Phan S.

art297
Opal | Level 21

I don't think your code does what you want. If I'm wrong, just ignore this post.

 

However, I think that the following does do what you want. Notice that I've changed your input dataset in order to capture all of the possible combinations. I accounted for this missing ID values while inputting the data.

 

data have (drop=x);
  retain id;
  do time=1 to 4;
    input x FD CL HS HL;
    if not missing(x) then ID=x;
    output; 
  end;
cards; 
1 1 . 0 . 
1 . 1 0 0
. . . 1 . 
1 0 0 0 1 
2 1 1 . .   
2 1 . 1 . 
. 1 . . 1  
2 . 1 1 0
2 . 1 0 1  
2 0 0 1 1  
. . . . .
3 1 1 1 .
3 1 1 . 1 
3 0 1 1 1
. . . . . 
4 1 1 1 1
5 0 . 0 1 
5 0 . 0 0  
. . . . .
5 0 0 . 0  
6 1 1 1 1  
6 1 1 1 1 
. . . . .
6 1 1 1 1
7 1 1 1 . 
7 1 1 0 . 
. . . . . 
7 1 1 0 1 
8 1 0 0 0 
8 0 0 1 1 
. . . . .
8 1 0 . 1
9 1 0 0 .  
9 0 0 0 . 
. . . . .
9 1 . 0 0
10 0 1 . 0 
10 1 1 0 .  
. . . . .  
10 1 1 0 1 
; 
run;

proc format;
  value anytwo
  2='1*2'
  3='1*3'
  4='1*4'
  6='2*3'
  8='2*4'
 12='3*4'
 ;
  value anythree
  6='1*2*3'
  8='1*2*4'
 24='2*3*4'
 ;
run;

data want (drop= i _:);
  set have;
  array items(4) FD CL HS HL;
  array _items(4);
  array needs(4) any_1bneed any_2bneed any_3bneed all_4bneed;
  format any_2bneed anytwo.;
  format any_3bneed anythree.;
  if nmiss(of items(*)) lt 4 then do;
    do i=1 to 4;
      _items(i)=items(i);
      needs(i)=0;
    end;
    if sum(of items(*)) eq 1 then any_1bneed=whichn(1,of items(*));
    else if sum(of items(*)) eq 2 then do;
      any_2bneed=whichn(1,of items(*));
      _items(any_2bneed)=0;
      any_2bneed=any_2bneed*whichn(1,of _items(*));
    end;
    else if sum(of items(*)) eq 3 then do;
      any_3bneed=whichn(1,of items(*));
      _items(any_3bneed)=0;
      any_3bneed=any_3bneed*whichn(1,of _items(*));
      _items(whichn(1,of _items(*)))=0;
      any_3bneed=any_3bneed*whichn(1,of _items(*));
    end;
    else all_4bneed=1;
  end;
run;

 

Art, CEO, AnalystFinder.com

PhanS
Obsidian | Level 7

Dear Art,

 

It appears to be what I am looking for.

 

Many thanks for your help.

 

Phan S.

 

Tom
Super User Tom
Super User

@PhanS wrote:

Hello PGStats,

 

I am looking for:

a) at least one out of the fourth items to be 1 (or yes)

2) all of the fourth items to be 1 (or yes)

3) at least 2 out of the four items to be 1 (yes)

4 at least 3  out of the four items to be 1 (yes)

 

My codes showed the results.

However, I would like to learn if there such SAS codes that can support mine. In the other words, to have the same results as my analyses.

 

Phan S.


Since it looks like your variables are coded as missing,0 or 1 then you just need to sum the values to answer those questions.

    n_yes =sum(of FD CL HS HL) ;
    at_least_one=1<= n_yes ;
    at_least_two=2 <= n_yes ;
    at_least_three=3 <= n_yes;
    all_one = 4= n_yes ;

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!

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
  • 9 replies
  • 1385 views
  • 3 likes
  • 5 in conversation