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

I am working through an problem where I have merged together numerous data sets and am trying to efficiently have SAS select which dataset to assign to a final set of variables. An example of the variables in the data set would be:

 

CoV1 Level1 Light1 Cov2 Level2 Light2 Cov3 Level3 Light3

 

Currently i am working through the data in a long form way with examples such as

 

IF CoV1 < Cov2 AND CoV1 < CoV3 THEN

     DO;

            CoV=CoV1

            Level=Level1

            Light=Light1

     END;

ELSE IF  CoV2 < CoV1 AND CoV2 < CoV3 THEN

    DO;

            CoV=CoV2

            Level=Level2

            Light=Light2

     END;

ELSE IF  CoV3 < CoV1 AND CoV3 < CoV2 THEN

    DO;

            CoV=CoV3

            Level=Level3

            Light=Light3

     END;

 

Obviously this long form programming poses problems problems when the number of merged files grows, I feel like there is a far more efficient way to program this type of decision in SAS, possibly using an array or other tools in the datastep.

 

Basically for each record i am trying to determine the minimum of CoV(1-3) variable and assign the CoV, Level and Light from that data set to the new variables CoV, Level and Light.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use the MIN/MAX  or SMALLEST/LARGEST functions if you want the smallest values.

WHICHN returns the index of the first occurrence of a single value.

 

array cov(3) cov1-cov3;
array level(3);
array light(3);

CoV=smallest(1, of (cov(*)); *find smallest value;
index = whichn(CoV, of cov(*)); *find index of smallest value;

Level = level(index);
Light = Light(index);
 

 

 

View solution in original post

2 REPLIES 2
Reeza
Super User

Use the MIN/MAX  or SMALLEST/LARGEST functions if you want the smallest values.

WHICHN returns the index of the first occurrence of a single value.

 

array cov(3) cov1-cov3;
array level(3);
array light(3);

CoV=smallest(1, of (cov(*)); *find smallest value;
index = whichn(CoV, of cov(*)); *find index of smallest value;

Level = level(index);
Light = Light(index);
 

 

 

Amir
PROC Star

Hi,

 

Have you tried the min function, e.g.:

 

cov = min(of cov1-cov3);

 

Regards,

Amir.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1061 views
  • 0 likes
  • 3 in conversation