BookmarkSubscribeRSS Feed
jerry898969
Pyrite | Level 9
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
10 REPLIES 10
Doc_Duke
Rhodochrosite | Level 12
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
jerry898969
Pyrite | Level 9
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
Cynthia_sas
SAS Super FREQ
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
jerry898969
Pyrite | Level 9
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.
Cynthia_sas
SAS Super FREQ
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):(Column).
[/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
jerry898969
Pyrite | Level 9
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
Ksharp
Super User
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
jerry898969
Pyrite | Level 9
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
NickR
Quartz | Level 8
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;
jerry898969
Pyrite | Level 9
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 10 replies
  • 4962 views
  • 0 likes
  • 5 in conversation