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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.