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.
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);
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);
Hi,
Have you tried the min function, e.g.:
cov = min(of cov1-cov3);
Regards,
Amir.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.