Hi, the purpose of this exercise is to concatenate these values. I'm still new to arrays, and was wondering if i can receive some help.
data have;
infile datalines dsd dlm=",";
input subject $ apple $ orange $ banana $ grape $ cherry $ jackfruit $;
datalines;
001, Y, , Y, , , Y
002, , , , Y, , Y
003, , , , , ,
;
run;
data want; set have; /* does not work*/
array tmp{*} apple--jackfruit;
want=catx(", ", of tmp:);
run;
The desired output i need is:
subject
001 apple, banana, jackfruit
002 grape, jackfruit
003
This should give you what you are looking for
data have;
infile datalines dsd dlm=",";
input subject $ apple $ orange $ banana $ grape $ cherry $ jackfruit $;
datalines;
001, Y, , Y, , , Y
002, , , , Y, , Y
003, , , , , ,
;
run;
data want(KEEP= subject want);
set have;
/* does not work*/
length want $100;
want='';
array tmp{*} apple--jackfruit;
do i=1 to dim(tmp);
want=catx(", ", want,ifc(tmp[i]='Y',vname(tmp[i]),''));
end;
run;
You appear to want the NAME of the variable and not the VALUE of the variable.
data want;
set have;
length want $200;
array tmp apple--jackfruit;
do index=1 to dim(tmp);
if tmp[index]='Y' then want=catx(', ',want,vname(tmp[index]));
end;
run;
Hello, @Hello_there 🙂
Even if your CATX function were to work, it would concatenate the values of 'Y' or missing values; it would not concatenate the name of the fruits because those are variable names, not variable values that CATX would work on. So you can't do it that way.
You need to loop through the different values in the array TMP, test to see if the value is 'Y', and then capture the name of the fruit from the variable name of the column where the 'Y' is located, and then concatenate. All of these steps are missing.
data want;
set have; /* does not work*/
array tmp{*} apple--jackfruit;
length want_result $ 200;
want_result=' ';
do i=1 to dim(tmp);
if tmp(i)='Y' then
want_result=catx(", ",want_result,vname(tmp(i)));
end;
drop i;
run;
@Hello_there wrote:
Hi, the purpose of this exercise is to concatenate these values. I'm still new to arrays, and was wondering if i can receive some help.
data have; infile datalines dsd dlm=","; input subject $ apple $ orange $ banana $ grape $ cherry $ jackfruit $; datalines; 001, Y, , Y, , , Y 002, , , , Y, , Y 003, , , , , , ; run; data want; set have; /* does not work*/ array tmp{*} apple--jackfruit; want=catx(", ", of tmp:); run;
The desired output i need is:
subject
001 apple, banana, jackfruit
002 grape, jackfruit
003
SAS tells you why it "doesn't work", just read the LOG which will look something like:
78 data want; set have; /* does not work*/ 79 array tmp{*} apple--jackfruit; 80 want=catx(", ", of tmp:); ---- 71 ERROR 71-185: The CATX function call does not have enough arguments. 81 run;
Two failures, one is you use a variable list TMP: which doesn't work because you have no variables whose names start with TMP. If you intend to use the values of the variables in the array the syntax would be:
data want; set have; /* does work for some definitions of "work" */ array tmp{*} apple--jackfruit; want=catx(", ", of tmp(*) ); run;
not the (*) following the Array name TMP. That is the way to use all values of the elements of the array, not TMP: .
This should give you what you are looking for
data have;
infile datalines dsd dlm=",";
input subject $ apple $ orange $ banana $ grape $ cherry $ jackfruit $;
datalines;
001, Y, , Y, , , Y
002, , , , Y, , Y
003, , , , , ,
;
run;
data want(KEEP= subject want);
set have;
/* does not work*/
length want $100;
want='';
array tmp{*} apple--jackfruit;
do i=1 to dim(tmp);
want=catx(", ", want,ifc(tmp[i]='Y',vname(tmp[i]),''));
end;
run;
Very helpful, thanks, AhmedAl_Attar!
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!
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.