BookmarkSubscribeRSS Feed
DOBBINHO
Obsidian | Level 7

Hi all,

I have a sample dataset of which i'd like to check if the observation starts with '/gb/pro/', and if it does, I need to concatenate it to the end of the previous observation with a ' ' in between.

Kindly advice on how I could do so....

 

LINES

chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/tx.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/tx.sas7bdat,new,add,read,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/vs.sas7bdat
/gb/pro/cli/dat/ba/102/009/stab/blin/le0/yc.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/vs.sas7bdat
/gb/pro/cli/dat/mb/102/009/stab/blin/le0/yc.sas7bdat,new,add,read,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/ba/102/009/stab/blin/le0/ye.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/ba/102/009/stab/blin/le0/ye.sas7bdat,new,add,read,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/ba/102/009/stab/blin/le0/yh.sas7bdat,old,xxxx,none,2/22/2008,9:51:7

7 REPLIES 7
Reeza
Super User

1. Use LAG to retrieve the previous value

2. Use =: to check if hte start of the variable matches your criteria

3. Use CATX() to concatenate the values.

 

 

Or since it looks like you're attempting to get file listings from an LS command, you could probably modify the code to generate the full file paths rather than doing this.

 

delete_lag.JPG


@DOBBINHO wrote:

Hi all,

I have a sample dataset of which i'd like to check if the observation starts with '/gb/pro/', and if it does, I need to concatenate it to the end of the previous observation with a ' ' in between.

Kindly advice on how I could do so....

 

LINES

chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/tx.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/tx.sas7bdat,new,add,read,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/vs.sas7bdat
/gb/pro/cli/dat/ba/102/009/stab/blin/le0/yc.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/mb/102/009/stab/blin/le0/vs.sas7bdat
/gb/pro/cli/data/mb/102/009/stab/blin/level0/yc.sas7bdat,new,add,read,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/mb/102/009/stab/blin/le0/ye.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/mb/102/009/stab/blin/level0/ye.sas7bdat,new,add,read,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/mb/102/009/stab/blin/level0/yh.sas7bdat,old,xxxx,none,2/22/2008,9:51:7


 

Astounding
PROC Star

I think LAG will be clumsy here.  You really need to look ahead, not look back.  Here is an approach:

 

data want;

length line $2000;  /* long enough length to hold double current length */

set have end=done;

if not done then set have (firstobs=2 keep=line rename=(line=next_line));

if done=0 and next_line =: '/gb/pro' then line = catx(' ', line, next_line);

if line =: '/gb/pro' then delete;

drop next_line;

run;

DOBBINHO
Obsidian | Level 7

set have end=done;

if not done then set have (firstobs=2 keep=line rename=(line=next_line));

if done=0 and next_line =: '/gb/pro' then line = catx(' ', line, next_line);

 

 

 

where is done coming from?

 

Astounding
PROC Star

It comes from the SET statement.  The END= option on a SET statement creates a variable, and automatically assigns it values of 0 or 1 to indicate whether that SET statement has read in all the observations available.

 

Take note ... it's an important tool to know about.

mkeintz
PROC Star

 

 

 

data want (drop=nxt_:);
  merge have
        have (firstobs=2 rename=(line=nxt_line));
  if nxt_line=:'/gb/pro' then line=catx(' ',line,nxt_line);
  if not(line=:'/gb/pro');
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Reeza
Super User

Oh...and you can modify an LS command to give you only paths from a certain folder.

PeterClemmensen
Tourmaline | Level 20

Something like this 

 

data have;
input string:$500.;
datalines;
chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/tx.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/tx.sas7bdat,new,add,read,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/vs.sas7bdat
/gb/pro/cli/dat/ba/102/009/stab/blin/le0/yc.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/dat/ba/102/009/stab/blin/le0/vs.sas7bdat
/gb/pro/cli/dat/mb/102/009/stab/blin/le0/yc.sas7bdat,new,add,read,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/ba/102/009/stab/blin/le0/ye.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/ba/102/009/stab/blin/le0/ye.sas7bdat,new,add,read,2/22/2008,9:51:7
chad,hardy,/gb/pro/cli/data/ba/102/009/stab/blin/le0/yh.sas7bdat,old,xxxx,none,2/22/2008,9:51:7
;

data want(keep=string);
	merge have have(firstobs=2 rename=(string=nextstring));
	if substr(nextstring, 1, 8)="/gb/pro/" then
		string=cats(string, " ", nextstring);
	if (substr(nextstring, 1, 8)="/gb/pro/") | (substr(string, 1, 8) ne "/gb/pro/");
run;

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!

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
  • 7 replies
  • 594 views
  • 1 like
  • 5 in conversation