I have a large number of variables for which I'd like to apply the same If then criteria:
if A1 = 4 or A1 = 5 then
A1_pos = 1;
if A1 = 3 or A1 = 2 or A1 = 1 then
A1_pos = 0;
if A1 = . then
A1_den = 0;
else A1_den = 1;
if A3 = 4 or A3 = 5 then
A3_pos = 1;
if A3 = 3 or A3 = 2 or A3 = 1 then
A3_pos = 0;
if A3 = . then
A3_den = 0;
else A3_den = 1;
Rather than write out a large number of if then statements, I'm thinking an array would could down on my code and be a more efficient way to do this. Any suggestions are greatly appreciated!
Thank you
That is exactly what arrays are designed for. From your code it looks like you have one input array and two output arrays. Note that I would change the naming of the output variables to have the numeric part as the suffix since then it is easier to use variable name lists.
array VAL A1-A4;
array DEN DEN_A1-DEN_A4 ;
array POS POS_A1-POS_A4 ;
Now just wrap your logic inside of a DO loop and replace your variable references with array references.
do i=1 to dim(VAL);
if val(i) in (4,5) then pos(i)=1;
else if val(i) in (1,2,3) then pos(i)=0;
if val(i)=. then den(i)=0
else den(i)=1;
end;
Something like this should get you started. Change the 10 the largest number of like variables. Assumes that you do NOT have a variable named "a" in the data. If you do you will need a different name for the first array. Also assumes that the A variables are consecutively numbered, if that is not the case then you need to provide a list that does match. Note that you can mix sequences and single values such as Array a a1-a10 a15 a20-a26;
data want; set have; array a a1-a10; /* assumes the Pos and Den variables do not already exist. NOTE that your SAS code will run much smoother if you use the suffix as the number and not in the middle if the variables already exist you will need to list them out or possibly A1_Pos -- A10_Pos will work if they are in order in the data set (yes that is two dashes)*/ array Pos_A {10}; array Den_a {10}; do i=1 to dim(a); if a[i] in (4,5) then Pos_A[i]=1; else if a[i] in (3,2,1) then Pos_A[i]=0; Den_A[i]= not missing(A[i]); end; run;
That is exactly what arrays are designed for. From your code it looks like you have one input array and two output arrays. Note that I would change the naming of the output variables to have the numeric part as the suffix since then it is easier to use variable name lists.
array VAL A1-A4;
array DEN DEN_A1-DEN_A4 ;
array POS POS_A1-POS_A4 ;
Now just wrap your logic inside of a DO loop and replace your variable references with array references.
do i=1 to dim(VAL);
if val(i) in (4,5) then pos(i)=1;
else if val(i) in (1,2,3) then pos(i)=0;
if val(i)=. then den(i)=0
else den(i)=1;
end;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.