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
Rhodochrosite | Level 12

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
Rhodochrosite | Level 12

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
Rhodochrosite | Level 12
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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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