data have;
input contract classe_201801 $ classe_201802 $ classe_201803 $ classe_201804 $ classe_201805 $ classe_201806 $
classe_201807 $ classe_201808 $ classe_201809 $ classe_201810 $ classe_201811 $ classe_201812 $ ;
datalines;
111111 A B B B A A A B B B B C
;
run;
%macro mc_histo_rece ;
%let N=1 ;
data want;
set have ;
array array_classe(12) classe_:;
%do i=1 %to 11 ;
%let suivant=%eval(&i+1) ;
if array_classe(&i) ne "B" and array_classe(&suivant) eq "B"
then do ;
begin_&N = substr(vname(array_classe(&suivant)),8,6) ;
%let N = %eval(&N + 1) ;
end ;
%end ;
run;
%mend ;
%mc_histo_rece ;Hello,
I need to obtain 2 new column, one column named begin_1 that holds 2018 and one column named begin_2 that holds 201808
because 201802 is the first begining of periode B and 201808 is the begining of the second periode B
thanks a lot in advance for your help
Nasser
Hi,
Do you mean something like that (it's more general when there may be more beginnings or none):
data have;
input contract classe_201801 $ classe_201802 $ classe_201803 $ classe_201804 $ classe_201805 $ classe_201806 $
classe_201807 $ classe_201808 $ classe_201809 $ classe_201810 $ classe_201811 $ classe_201812 $ ;
datalines;
111111 A B B B A A A B B B B C
222222 B B B B B B B B B B B C
333333 A A A A A A A A A A A A
444444 A B A B A B A B A B A B
555555 B A B A B A B A B A B A
;
run;
data want;
set have ;
_fake_ = " ";
array array_classe[0:12] _fake_ classe_:;
array begin_[6] $ 32;
j=0;
do i = 1 to hbound(array_classe);
if array_classe[i] = "B" and array_classe[i-1] ne "B" then
do;
j+1;
begin_[j] = vname(array_classe[i]);
end;
end;
drop i j _fake_;
run;
All the best
Bart
@Nasser_DRMCP wrote:
Hello,
I need to obtain 2 new column, one column named begin_1 that holds 2018 and one column named begin_2 that holds 201808
because 201802 is the first begining of periode B and 201808 is the begining of the second periode B
thanks a lot in advance for your help
Nasser
Why do you need new variables? Assuming that by 201802 you mean the variable classe_201802 you already have the variable. And since you are only asking to add new variables with apparently the existing value then there is not reason to add variables.
Also your subject line says "changing between columns". So what do you want to check for changing? What does the expected output for example data look like?
And your macro is almost certainly not needed. A simple
array begin (12) $6. ;
do I= 1 to dim(array_classe);
if array_classe(ii) ne "B" and array_classe(I+1) eq "B" then begin(I) = substr(vname(array_classe(I+1)),8,6);
end;
Though you should be able to calculate the value of begin from the value of the I index variable as well.
Hi,
Do you mean something like that (it's more general when there may be more beginnings or none):
data have;
input contract classe_201801 $ classe_201802 $ classe_201803 $ classe_201804 $ classe_201805 $ classe_201806 $
classe_201807 $ classe_201808 $ classe_201809 $ classe_201810 $ classe_201811 $ classe_201812 $ ;
datalines;
111111 A B B B A A A B B B B C
222222 B B B B B B B B B B B C
333333 A A A A A A A A A A A A
444444 A B A B A B A B A B A B
555555 B A B A B A B A B A B A
;
run;
data want;
set have ;
_fake_ = " ";
array array_classe[0:12] _fake_ classe_:;
array begin_[6] $ 32;
j=0;
do i = 1 to hbound(array_classe);
if array_classe[i] = "B" and array_classe[i-1] ne "B" then
do;
j+1;
begin_[j] = vname(array_classe[i]);
end;
end;
drop i j _fake_;
run;
All the best
Bart
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.