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
Hello, data have; input jour DDMMYY10. ; format jour DDMMYY10. ; datalines; 01/01/2022 02/01/2022 03/01/2022 04/01/2022 05/01/2022 06/01/2022 07/01/2022 08/01/2022 09/01/2022 10/01/2022 11/01/2022 ; proc sql; select distinct jour into :liste_jour separated by ' ' from have ; quit ; %put &liste_jour; by executing this code above the result is 01/01/2022 02/01/2022 03/01/2022 04/01/2022 05/01/2022 06/01/2022 07/01/2022 08/01/2022 09/01/2022 10/01/2022 11/01/2022 But my problem is when I try to get each date value for loop like this below; the result is not corret because it is 01 and 01 and 2021... thanks in advance for your help kind regards Nass %macro mc_boucle_surlesjours ; %local i next_jour; %let i=1; %do %while (%scan(&liste_jour, &i) ne ""); %let next_jour = %scan(&liste_jour, &i); %put -->&next_jour ; %let i = %eval(&i + 1); %end ; %mend ; %mc_boucle_surlesjours ;
1 ACCEPTED SOLUTION

Accepted Solutions
Nasser_DRMCP
Lapis Lazuli | Level 10

ok I found the solution.see below. in the scan I forgot to specify the separator space. this code below works as I intended 

%macro mc_boucle_surlesjours ;
 %local i next_jour;
 %let i=1;
 %do %while (%scan(&liste_jour, &i, " ") ne );
   %let next_jour = %scan(&liste_jour, &i, " ");
   %put -->&next_jour ;
   %let i = %eval(&i + 1);
 %end ; 
%mend ;

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

Please re-format your problem description to make it readable. Thanks.

--
Paige Miller
Nasser_DRMCP
Lapis Lazuli | Level 10
I have a table with a variable "day" that containes dates values like "01/01/022" "02/01/2022" "03/01/2022" … I would that a macro variable gets each values by usinf a loop
PaigeMiller
Diamond | Level 26

Repeating:

 

Please re-format your problem description to make it readable.

--
Paige Miller
Nasser_DRMCP
Lapis Lazuli | Level 10
I would like to write a loop step with the objectif is to put into a variable the values 01/01/2022 for iteration 1 02/01/2022 for iteration 2 03/01/2022 for iteration 3 04/01/2022 ... 05/01/2022 06/01/2022 07/01/2022 08/01/2022 09/01/2022 10/01/2022 11/01/2022 for the last iteration
Nasser_DRMCP
Lapis Lazuli | Level 10
by executing this %macro mc_boucle_surlesjours ; %local i next_jour; %let i=1; %do %while (%scan(&liste_jour, &i) ne ); %let next_jour = %scan(&liste_jour, &i); %put -->&next_jour ; %let i = %eval(&i + 1); %end ; %mend ; %mc_boucle_surlesjours ; the result is -->01 -->01 -->2022 -->02 -->01 -->2022 -->03 -->01 -->2022 -->04 -->01 -->2022 -->05 -->01 -->2022 -->06 -->01 -->2022 -->07 ...
Nasser_DRMCP
Lapis Lazuli | Level 10

ok I found the solution.see below. in the scan I forgot to specify the separator space. this code below works as I intended 

%macro mc_boucle_surlesjours ;
 %local i next_jour;
 %let i=1;
 %do %while (%scan(&liste_jour, &i, " ") ne );
   %let next_jour = %scan(&liste_jour, &i, " ");
   %put -->&next_jour ;
   %let i = %eval(&i + 1);
 %end ; 
%mend ;
PaigeMiller
Diamond | Level 26

@Nasser_DRMCP wrote:
by executing this %macro mc_boucle_surlesjours ; %local i next_jour; %let i=1; %do %while (%scan(&liste_jour, &i) ne ); %let next_jour = %scan(&liste_jour, &i); %put -->&next_jour ; %let i = %eval(&i + 1); %end ; %mend ; %mc_boucle_surlesjours ; the result is -->01 -->01 -->2022 -->02 -->01 -->2022 -->03 -->01 -->2022 -->04 -->01 -->2022 -->05 -->01 -->2022 -->06 -->01 -->2022 -->07 ...

Do not post code as plain unformatted text. Post code in the box that appears when you click on the "Insert SAS Code" icon. You may have found the correct answer, but again, I ask you to re-format it to be readable by using the "Insert SAS Code" icon, for the benefit of everyone else.

--
Paige Miller
ballardw
Super User

Read your post and then look at this:

data have;
 input jour DDMMYY10. ;
 format jour DDMMYY10. ;
 datalines;
01/01/2022
02/01/2022
03/01/2022
04/01/2022
05/01/2022
06/01/2022
07/01/2022
08/01/2022
09/01/2022
10/01/2022
11/01/2022
;
 proc sql noprint;
 select distinct jour into :liste_jour separated by ' ' from have ;
 quit ;
 %put &liste_jour;


%macro mc_boucle_surlesjours ;
 %local i next_jour;
 %do i = 1 %to %sysfunc(countw(&liste_jour,%str( )));
 
    %let next_jour = %scan(&liste_jour, &i,%str( ));
    %put -->&next_jour ;

 %end ;
 %mend ;
 %mc_boucle_surlesjours ;
 

That is part of what is meant by "reformat your post".

The above does part of what I think you want.

Problem: using SCAN by default will treat the / character as delimiter between words. The fix above forces a space into the parameters for Countw and Scan functions as the only delimiter.

Or depending on how you expect to use those dates do not apply a format. Really the only time a macro Date value should be formatted is when it is read my humans such as in a title, footnote or possibly a file name.

 

If you do not format the date variable you will get the numbers that represent the date and can be used if comparisons or other date related functions and would not need the forced space from the %Str

Nasser_DRMCP
Lapis Lazuli | Level 10
thank you very much very clear and very interesting

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 465 views
  • 3 likes
  • 3 in conversation