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;
You are missing some important pieces / understanding
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 ;
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,
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.
You are missing some important pieces / understanding
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 ;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.