BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
4 REPLIES 4
DanielSantos
Barite | Level 11
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
deleted_user
Not applicable
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
deleted_user
Not applicable
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;
deleted_user
Not applicable
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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1711 views
  • 0 likes
  • 2 in conversation