Help using Base SAS procedures

Help with strings and retain

Reply
Super Contributor
Posts: 400

Help with strings and retain

I have the following values and I can't seem to get the end result

VAR FLAG1 FLAG2 Text
Test1 1 7. This is test string
Test1 1 12. This is test string
Test1 2 This line needs to be added to previous line
Test2 1 55. This is test string
Test2 2 This line needs to be added to previous line

My final result should be

Test1 1 7. This is test string
Test1 1 12. This is test string This line needs to be added to previous line
Test2 1 55. This is test string This line needs to be added to previous line

This is my program
data temp1 ;
set temp0 ;
by var flag1 ;

retain text2 ;
if first.flag1 and find(flag3, '.') > 0 then text2 = text ;
if last.flag1 then text2 = text2 || text ;
run ;

Any help would be greatly appreciated. I think i'm missing something simple but I can't pin point it.

thank you
Trusted Advisor
Posts: 2,115

Re: Help with strings and retain

Posted in reply to jerry898969
A couple of things that you need to look at:

Flag3 does not appear to be defined.
Print out temp0; the data you list for the flag1=2 doesn't look right.

Doc Muhlbaier
Duke
Super Contributor
Posts: 400

Re: Help with strings and retain

Duke,

Flag3 is actually flag2. The rows with flag1=2 need to be rolled up into the text with the previous row. I've worked with retain before but for some reason this is giving me the hardest time.

Thanks for your help
SAS Super FREQ
Posts: 8,866

Re: Help with strings and retain

Posted in reply to jerry898969
Hi:
Let's verify what WORK.TEMP0 looks like before we go down the road of blaming poor old RETAIN for causing problems. Can you explain what is the purpose of the FLAG2 variable and why it has values on some variables and not on others??? Is FLAG2 character or numeric??? Based on what you posted, and assuming that FLAG2 is numeric (and not character), this is what I think that TEMP0 looks like (showing all the variables "lined up" helps me visualize what every observation looks like):
[pre]
what is in WORK.TEMP0

var flag1 flag2 text

Test1 1 7 This is test string
Test1 1 12 This is test string
Test1 2 . This line needs to be added to previous line
Test2 1 55 This is test string
Test2 2 . This line needs to be added to previous line
[/pre]

Can you also explain why your desired output has this:
[pre]
Test1 1 7. This is test string
Test1 1 12. This is test string This line needs to be added to previous line
Test2 1 55. This is test string This line needs to be added to previous line
[/pre]

I don't understand why there are 2 Test1 observations, but only 1 Test2 observation. Also in your original data you show values for FLAG2 of 7. 12. and 55. -- is the '.' significant?????

Can you elaborate on WORK.TEMP0 and the desired results??

cynthia
Super Contributor
Posts: 400

Re: Help with strings and retain

Posted in reply to Cynthia_sas
Cynthia,

Thank so much for your reply.
What you have for TEMP0 is exactly what I have except that Flag2 is character and the "." is needed. This is based on a big file i'm working with. I'm trying to crate a dataset based on certain flags. What is happeneing in my file is that the text is wraping to another line where flag2 = missing. So that row has to be rolled up with the previous lines text. What is happening is that I was given a really big text file and I have to find a way to pull out specific portions of that file. That i'm close too, it's the wrapping the text from the previous line that was giving me a really hard time.

This is the code I finally think I have working

data temp1 ;
length lngstr $ 200 ;
set temp0 ;
retain lngstr " " ;

if flag2 ne . then lngstr = text ;
else if flag2 = 1 then lngstr = catx(' ',lngstr,text);
run ;

Thank you again for all your help on this and other questions i've asked.
SAS Super FREQ
Posts: 8,866

Re: Help with strings and retain

Posted in reply to jerry898969
Hi:
If FLAG2 is CHARACTER, then this statement
[pre]
if flag2 ne . then lngstr = text ;
else if flag2 = 1 then lngstr = catx(' ',lngstr,text);
[/pre]

should be causing a note in the log:
[pre]
NOTE: Character values have been converted to numeric values at the places given by: (Line)Smiley SadColumn).
[/pre]

This IF statement is forcing FLAG2 to be treated as a NUMBER, not a CHARACTER -- it might not make any difference for a simple test like what you're doing, but it is better to get these concepts straight from the beginning.

However, I see a different problem. In the data that I posted, the values for FLAG2 are 7, 12, 55 and . or as character strings they would be '7', '12', '55' and '.'. None of the FLAG2 variables has a value of 1. On the other hand, FLAG1 has values of 1 and 2, so I'm wondering whether you've posted working code.

cynthia
Super Contributor
Posts: 400

Re: Help with strings and retain

Posted in reply to Cynthia_sas
Cynthia,

you are correct. That code should have been

if flag2 ne . then lngstr = text ;
else lngstr = catx(' ',lngstr,text);

If flag2 is "." that means it should be added to the end of the line above it.

Thanks again for you time and help. I work with SAS but not on a consistent basis as I would like so I'm not able to grasp it like I would like. That help here is truly a gift.

Thank You
Super User
Posts: 10,028

Re: Help with strings and retain

Posted in reply to jerry898969
Hi.
Just as Cynthia said,If flag2 is character ,why using ' flag2 ne . ' ? Your data structure is ambiguous.

[pre]

data temp0;
input var $ flag1 flag2 $3. text & $50.;
datalines;
Test1 1 7. This is test string
Test1 1 12. This is test string
Test1 2 This line needs to be added to previous line
Test2 1 55. This is test string
Test2 2 This line needs to be added to previous line
run;
proc sort data=temp0;
by var;
run;
data temp1 index(keep=var text rename=(text=_text));
set temp0;
by var;
if last.var then do;
output index;
delete;
end;
output temp1;
run;
data temp2;
merge temp1 index;
by var;
run;
data op(drop=text _text);
set temp2;
by var;
length __text $ 100;
__text=text;
if last.var then __text=catx(' ',text,_text);
run;
proc print noobs;run;

[/pre]



Ksharp
Super Contributor
Posts: 400

Re: Help with strings and retain

KSharp,

Thank you for your post. I corrected the issue you mention in your post and finally have it working.

Thank you for your help

Jerry
Frequent Contributor
Posts: 81

Re: Help with strings and retain

Posted in reply to jerry898969
I think you need to get rid of leading and trailing blanks....

if last.flag1 then text2 = strip(text2) || strip(text) ;

Not sure if I undertood your problem correctly, but below code should give you the desired output...

data one;
length VAR Text $100;
var = 'Test1'; flag1=1; flag2=7; text='This is test string'; output;
var = 'Test1'; flag1=1; flag2=12; text='This is test string'; output;
var = 'Test1'; flag1=2; flag2=.; text='This line needs to be added to previous line'; output;
var = 'Test2'; flag1=1; flag2=55; text='This is test string'; output;
var = 'Test2'; flag1=2; flag2=.; text='This line needs to be added to previous line'; output;
run;

proc sort; by var descending flag1 descending flag2; run;

data two;
length pretext newtext $100;
set one;
by var descending flag1 descending flag2;
retain pretext preflag;
if first.var then do; pretext=''; preflag=.; end;
if flag1=1 and preflag=2 then newtext=strip(text)||strip(pretext);
else newtext=strip(text);
pretext=text;
preflag=flag1;
if flag1=1;
drop preflag pretext;
run;

proc sort; by var flag1 flag2; run;
Super Contributor
Posts: 400

Re: Help with strings and retain

Nick,

Thank you for your solution as well. That worked just like I wanted it too. I'm going to spend some time looking at it and clearing up how I have to handle strings with the retain statement.

Thank you again for your help

Jerry
Ask a Question
Discussion stats
  • 10 replies
  • 2213 views
  • 0 likes
  • 5 in conversation