BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
AhmedAl_Attar
Ammonite | Level 13

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;

View solution in original post

10 REPLIES 10
Tom
Super User Tom
Super User

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;
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
Hello_there
Lapis Lazuli | Level 10
Hey, @PaigeMiller ! Thanks for the explanation and for your help again. Arrays are still tricky for me
ballardw
Super User

@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: .

Hello_there
Lapis Lazuli | Level 10
Thanks for breaking it down for me, ballardw!
AhmedAl_Attar
Ammonite | Level 13

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;
Hello_there
Lapis Lazuli | Level 10
Thanks for this eloquent solution, could you explain to me the purpose of doing want = "" after the length statement in your code?
AhmedAl_Attar
Ammonite | Level 13
The Length Statement is executed once during the compilation phase, which in turn, would declare the 'want' variable in the PDV, and assign it a blank.

As you loop through the data set records via the SET statement, you want to make sure to explicitly reset the 'want' variable back to blank, before using in the catx function within the loop.

Hope this helps

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 1015 views
  • 6 likes
  • 5 in conversation