DATA Step, Macro, Functions and more

Concat values to the values from the previous row(s).

Reply
N/A
Posts: 0

Concat values to the values from the previous row(s).

Hi All,

I'm trying to use the retain method to create concatenated variables.

While the cnt is less than 12 retain and concat each KVar to the KVar of the next row. Then once 12 is equaled, create kVarKeep, clear the value of Temp, then continue to the end of the data set.

This what I'm trying to get:
KNum KVar LKVars Cnt KVarKeep
1 Var1 4 4 .
2 Var2 4 8 .
3 Var3 4 12 Var1 Var2 Var3
4 Var4 4 4 .
5 Var5 4 8 .
6 Var6 4 12 Var4 Var5 Var6
7 Var7 4 4 .
8 Var8 4 8 Var7 Var8


I'm trying to do this because of the limitations in the # of characters that can be placed into a single marco variable. I have an AF frame that allows users to select fields from a list box, when the user selects more than the single variable can handle the submission to the rsubmit data step fails.
Super Contributor
Posts: 474

Re: Concat values to the values from the previous row(s).

You're in right direction, the retain method will do the job.

You just need to allocate KVarKeep big enough through the LENGTH statement.

http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000218807.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt
N/A
Posts: 0

Re: Concat values to the values from the previous row(s).

I am not pretty clear on this. But I have tried the following code. Would this solution be helpful ??

data test1(drop=temp1 temp2);
set test;
length KVarKeep $50 temp1 temp2 $50;
retain temp1 temp2;
if temp1 ne '' then temp2=catx(' ', temp2, KVar);
temp1=KVar;
if temp2='' then temp2=temp1;
if cnt=12 then do;
KVarKeep=temp2;
temp2='';
end;
put _all_;
run;

~ Sukanya E
N/A
Posts: 0

Re: Concat values to the values from the previous row(s).

Thanks everyone for your help.

Here is what is working. My only remaining problem is that the Name variable from the proc contenst comes over with a size of 32, and the extra spaces are throwing off my concatenated variables, since my Cnt of Length is only counting the character values. My queastions now is how do I get it to only taka the chars, not the empty spaces?

Proc Contents Data=HMD.HMDFull Out=Work.HMDChars;
Title ' ';
Run;

Data Test_Keeps (Keep=KVar);
Set HMDChars;
KVar=Name;
Run;

Data Test_Keeps_2;
Set Test_Keeps end=last;
Length Temp KeepVars $ 150;
Retain Cnt 0;
Retain Temp;
LKVars=Length(KVar);
Cnt+LKVars;
If Cnt gt 120 Then
Do;
KeepVars=Temp;
Cnt=LKVars;
Temp=KVar;
End;
Else If Cnt eq 120 Then
Do;
Temp=KVar||Temp;
KeepVars=Temp;
Cnt=0;
Temp=" ";
End;
Else If Cnt lt 120 Then
Do;
Temp=KVar||Temp;
End;

If last Then
Do;
KeepVars=Temp;
Temp=" ";
End;
Run;
N/A
Posts: 0

Re: Concat values to the values from the previous row(s).

Just answered my own questions.

I'm embedding the Catx function to take only the chars and add the space separator.

sp=' ';

Temp=catx(sp,KVar,Temp)

Thank you again to everyone.
Ask a Question
Discussion stats
  • 4 replies
  • 185 views
  • 0 likes
  • 2 in conversation