Replace first occurrence of a column with new character string

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

Replace first occurrence of a column with new character string

Hello,

 

So I have a data set where I want to replace the first occurrence of a specific character string with a new charater string and make all other occurrences of this string blank.

 

My data looks something like

 

Day    Month  

1        Mar

2        Mar

3        Mar

4        Mar

5        Mar

1        Apr

2        Apr

 

 

etc. and I want it to look like

 

Day    Month

1         March

2

3

4

5

1       April

2

 

because I am exporting it to excel and the cell values get auto reported to a table I am using for an exhibit.

 

What I have tried so far is

 

data test11; set test; if first.month="Mar" then month2="March";run;

 

After this I was planning on then just subbing in the month2 variable or something by renaming, but the first. statement isn't working how I thought it would. Any help would be great. Thanks

 

 

 

 


Accepted Solutions
Solution
‎10-23-2015 03:23 PM
Valued Guide
Posts: 858

Re: Replace first occurrence of a column with new character string

Here is a solution:

 

data have;
input Day    Month$;
cards;
1        Mar
2        Mar
3        Mar
4        Mar
5        Mar
1        Apr
2        Apr
;

data want;
set have;
by month notsorted;
if not first.month then month = '';
run;

View solution in original post


All Replies
Contributor
Posts: 39

Re: Replace first occurrence of a column with new character string

Nevermind, I actually figured this out. I did

 

data test11; set test; by month;if first.month=1 then month2=1;run;

data test12; set test11; if month="Mar" and month2=1 then month3="March"
;run;

Solution
‎10-23-2015 03:23 PM
Valued Guide
Posts: 858

Re: Replace first occurrence of a column with new character string

Here is a solution:

 

data have;
input Day    Month$;
cards;
1        Mar
2        Mar
3        Mar
4        Mar
5        Mar
1        Apr
2        Apr
;

data want;
set have;
by month notsorted;
if not first.month then month = '';
run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 275 views
  • 0 likes
  • 2 in conversation