BookmarkSubscribeRSS Feed
Ammu18
Calcite | Level 5

I have a dataset of 360 days 

I want to convert day 1 to day 30 as month 1, day 31 to day 60 as month 2 and likewise rename till month 12

No date is given

Please help me to solve this

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Show us a portion of this data set.

 

What have you tried so far?

--
Paige Miller
Ammu18
Calcite | Level 5

The data set looks like

 

day1 day2  day3 ......day359  day360.   

1         0        1               1                1

1         0        0               1                1

0          1       1                0               0

 

 

 

1. I tried to rename the variables 

But only day1 change to month1. 

2. then i tried the code given below

%macro rename1(oldvarlist, newvarlist);
  %let k=1;
  %let old = %scan(&oldvarlist, &k);
  %let new = %scan(&newvarlist, &k);
     %do %while(("&old" NE "") & ("&new" NE ""));
      rename &old = &new;
	  %let k = %eval(&k + 1);
      %let old = %scan(&oldvarlist, &k);
      %let new = %scan(&newvarlist, &k);
  %end;
%mend;data a ;
  set b;
  %rename1(day1 day2...day30, month1); /*****didnt work as only day 1 changed********/
run;

 

PaigeMiller
Diamond | Level 26

@Ammu18 wrote:

The data set looks like

 

day1 day2  day3 ......day359  day360.   

1         0        1               1                1

1         0        0               1                1

0          1       1                0               0


Okay, and what should the end result look like after you do this renaming?

--
Paige Miller
Ammu18
Calcite | Level 5

I have 360 days given and it should be renamed to month1 to month 12

I have to consider 30mdays in each month

month1 month1  month1 ......month12 month12.   

1         0        1               1                1        1

1         0        0               1                1        1

0          1       1                0               0         1

PaigeMiller
Diamond | Level 26

This is not possible in SAS. You cannot have two (or more) different columns of data with the exact same variable name.

 

Perhaps you need to re-think the problem.

 

 

--
Paige Miller
Ammu18
Calcite | Level 5

Thanks a lot for your feedback PaigeMiller.

I really appreciate your prompt response.

 

--

Ammu

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Ammu18 

 

Never use macro coding if it is not necessary, and it is not in this case. As explained by @PaigeMiller, you  cannot have more than one variable with the same name, and I cannot imagine why you would want that, because you would still have the same output data set with 360 variables. I think your request only makes sense it you want to output 12 output variabeles with Month1 as the sum of day1-day30 and so on. If that is a usable result, you can get it in one data step using 2 arrays like this:

 

data have (drop= i j);
	array day day1-day360;
	do i = 1 to 10;
		do j = 1 to 360;
			day{j} = ranuni(1) > 0.5;
		end;
		output;
	end;
run;

data want (keep=month1-month12); set have;
	array day day1-day360;
	array month month1-month12;
	do i = 1 to 360;
		m = int((i-1)/30)+1;
		month{m} = sum(month{m},day{i});
	end;
run;

Want has 360 variables with random distribution of 0/1, and have is:

 

 

 

random.gif

Ammu18
Calcite | Level 5

Hi @ErikLund_Jensen,

Thanks a lot for the feedback. I will try this.

 

--

Ammu

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1844 views
  • 0 likes
  • 3 in conversation