Hi experts,
I have a list of variables where clinical responses have been captured for every clinical events happened to multiple patient came for diagnosis.
there are about 35 variables named differently.
subjid klebsiella pneumonia acetobacter choristidium_diff ......etc (22 variables)
0021 X X X
0022 X X X X
0024 X X X
0025 X X X X
0028 X X X X
......... continued till 10,000 paticipants
I would like to make a new column which will be 1 if all the 22 columns have X. if there is blank in any of the 22 columns then it has be 0.
I thought of using arrays to solve this and written a below code, but doesn't seems to execute with the aim. IF anyone can help with modifying the code, that would be helpful.
array infection{*} klebsiella pneumonia acetobacter choristidium_diff acetobactor;
do i=1 to dim(infection);
if infection{i} ="X" then inf=1;else inf=0;
end;
I strongly suggest that when you have variables that only take one value of interest that you code them as numeric with 1 as the value of interest.
Then this question becomes trivial because you could SUM the variables and if the sum is 22 then "all" have the value.
When you have character variables you have to COUNT how many have the characteristic
Something like:
count=0;
array infection{*} klebsiella pneumonia acetobacter choristidium_diff acetobactor;
do i=1 to dim(infection);
if infection{i} ="X" then count=count+1;
end;
/* AFTER the loop see what your count is*/
if count= dim(infection) then inf=1;
else 0.
If your diagnoses are in consecutive order on the data set, it is very easy to refer to them, e.g. as klebsiella--acetobactor. If they are blank if not "X", you can use CATS and LENGTHN to get the number of X-es:
data want;
set have;
inf=lengthn(cats(of klebsiella--acetobactor))=22;
run;
I used LENGTHN instead of LENGTH, so that the function will return a 0 (LENGTH returns 1 for an empty string) when there are no diagnoses, just in case you also want the actual number of diagnoses.
data want; set have; array infection{*} klebsiella pneumonia acetobacter choristidium_diff acetobactor; if ' ' in infection then inf=0; else inf=1; run;
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: