BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mballey
Calcite | Level 5

 

Good Day!

PROBLEM:

My code is looping through a list of inputStrings. Each inputString has a corresponding Indicator.

If the inputString Indicator is "S" then store it in outputString1

If the inputString  Indicator is not "S" then store it in outputString2

I want to store the outputs of each loop iteration into a single variable in a concatenated format separated by "/". Please see below:

 

Thank you so much!

 

HAVE:

(loop 1)

inputString Indicator

inputString1 S

inputString2 A

inputString3 B

inputString4 C

inputString5 S

 

WANT:

outputString1= inputString1/inputString5

outputString2= inputString2/inputString3/inputString4

 

SAMPLE CODE:
data WORK.have / view=WORK.have;
infile "&path" length=len missover;

curr_len=1;
    do until (condition);

      select (Type);
      when ("M")
     select (Indicator);
         When ("S");
             do;
                 outputString1=Trim(inputString);
                 output;
            end;

 

       otherwise
            do;
                outputString2=Trim(inputString) || ' / ';
           output;

 

end;
end;
end;
end;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

You are missing some important pieces / understanding

  • INPUT statement to read values from the data file
  • RETAIN statement to maintain the values of partially computed strings over iterations of the implicit  DATA step loop.
  • You are not using the information provided by the LENGTH=<variable-name> option.  Not sure why curr_len is in the code.
  • SAS has several very useful concatenation functions.  The concatenation operator, ||, is used less frequently in (my) code I write today.
  • INFILE option EOF=<label> can be used to redirect program flow at the 'end of data' condition.
  • DATA Step character variable are not dynamically lengthed.  You should declare a character variable as $<length> prior to using it.
    • Experienced SAS coders will understand how character variables obtain their Program Data Vector (PDV) length from the context of their first occurrence in the code and experientially know how long the variable will be (and thus may not code explicitly a LENGTH statement).

Example program:

data want(keep=builtString1 builtString2);
  infile cards eof=lastcard;

  length string $20 flag $1;
  input string flag ;

  length builtString1 builtString2 $200;
  retain builtString1 builtString2 ;

  if flag = 'S' then 
    builtString1 = catx('/',builtString1,string);
  else
    builtString2 = catx('/',builtString2,string);

  return;  /* stay out of labelled section of code */

lastcard:
  OUTPUT;
cards;
inputString1 S
inputString2 A
inputString3 B
inputString4 C
inputString5 S
;

 

View solution in original post

4 REPLIES 4
ed_sas_member
Meteorite | Level 14

Hi @mballey 

 

here is an approach to do this:

 

data want;
	set have end=eof;
	length outputString1 outputString2 $ 200;
	retain outputString1;
	retain outputString2;
	if Indicator="S" then outputString1 = catx("/",outputString1,inputString);
	else outputString2 = catx("/",outputString2,inputString);
	if eof then output;
	keep outputString1 outputString2;
run;

/*optional */
proc transpose data=want out=want2 (rename=(col1 = inputstrings)) name=outputstring;
	var outputString1 outputString2;
run;

Best,

s_lassen
Meteorite | Level 14

Here is code to give you your wanted output from the input shown:

data have;
  length InputString $20 Indicator $1;
  input InputString Indicator;
cards;
inputString1 S
inputString2 A
inputString3 B
inputString4 C
inputString5 S
;run;

data want;
  length InputString1 InputString2 $200;
  do until(done);
    set have end=done;
    select(indicator);
      when('S') call catx('/',InputString1,InputString);
      otherwise call catx('/',InputString2,InputString);
      end;
    end;
  keep InputString1 InputString2;
run;

From the code you presented, it seems there is something more to it, but I am not sure what.

RichardDeVen
Barite | Level 11

You are missing some important pieces / understanding

  • INPUT statement to read values from the data file
  • RETAIN statement to maintain the values of partially computed strings over iterations of the implicit  DATA step loop.
  • You are not using the information provided by the LENGTH=<variable-name> option.  Not sure why curr_len is in the code.
  • SAS has several very useful concatenation functions.  The concatenation operator, ||, is used less frequently in (my) code I write today.
  • INFILE option EOF=<label> can be used to redirect program flow at the 'end of data' condition.
  • DATA Step character variable are not dynamically lengthed.  You should declare a character variable as $<length> prior to using it.
    • Experienced SAS coders will understand how character variables obtain their Program Data Vector (PDV) length from the context of their first occurrence in the code and experientially know how long the variable will be (and thus may not code explicitly a LENGTH statement).

Example program:

data want(keep=builtString1 builtString2);
  infile cards eof=lastcard;

  length string $20 flag $1;
  input string flag ;

  length builtString1 builtString2 $200;
  retain builtString1 builtString2 ;

  if flag = 'S' then 
    builtString1 = catx('/',builtString1,string);
  else
    builtString2 = catx('/',builtString2,string);

  return;  /* stay out of labelled section of code */

lastcard:
  OUTPUT;
cards;
inputString1 S
inputString2 A
inputString3 B
inputString4 C
inputString5 S
;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 618 views
  • 4 likes
  • 4 in conversation