Hi, I am trying to repeat the same value for all similar dates.
My data looks like this, where first column is month and year and second column is a percentage:
month-year monthly-com
03M2015 0.57
03M2015
03M2015
04M2015 0.63
04M2015
05M2015 0.73
05M2015
05M2015
The values of second column should be repeated for all similar month-year. I tried the below code but it does not work. Anybody can help? Thanks!
data outputData (drop=temp_Monthly_com temp_month_year);
set have;
retain temp_Monthly_com . temp_month_year .;
if temp_month_year ne month_year then do;
temp_month_year = month_year;
format temp_month_year MMYY.;
temp_Monthly_com = .;
end;
if Monthly_com = . then temp_Monthly_com=Monthly_com;
else temp_Monthly_com = Monthly_com;
run;
data want;
set have;
retain monthly_com_filled;
if not missing(monthly_com) then monthly_com_filled = monthly_com;
run;
Replace Old with your old variable name and new with your new variable name.
data have;
infile cards truncover;
input month_year $ monthly_com;
cards;
03M2015 0.57
03M2015
03M2015
04M2015 0.63
04M2015
05M2015 0.73
05M2015
05M2015
;
data want;
update have(obs=0) have;
by month_year;
output;
run;
Thanks for quick reply. I run your code but still the same problem.
data want;
set have;
retain monthly_com_filled;
if not missing(monthly_com) then monthly_com_filled = monthly_com;
run;
Replace Old with your old variable name and new with your new variable name.
Thanks a lot!
Not sure where is the discrepancy unless you post the log and the result you are getting. Here is my test and results
data have;
infile cards truncover;
input month_year $ monthly_com;
cards;
03M2015 0.57
03M2015
03M2015
04M2015 0.63
04M2015
05M2015 0.73
05M2015
05M2015
;
data want;
update have(obs=0) have;
by month_year;
output;
run;
proc print noobs;run;
month_year | monthly_com |
---|---|
03M2015 | 0.57 |
03M2015 | 0.57 |
03M2015 | 0.57 |
04M2015 | 0.63 |
04M2015 | 0.63 |
05M2015 | 0.73 |
05M2015 | 0.73 |
05M2015 | 0.73 |
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: