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
Show us a portion of this data set.
What have you tried so far?
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;
@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?
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
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.
Thanks a lot for your feedback PaigeMiller.
I really appreciate your prompt response.
--
Ammu
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:
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.
Ready to level-up your skills? Choose your own adventure.