BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nasser_DRMCP
Lapis Lazuli | Level 10
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

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
ballardw
Super User

@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.

 

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 2 replies
  • 431 views
  • 0 likes
  • 3 in conversation