BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Onizuka
Pyrite | Level 9

Hello !

 

Sorry for my english..

 

How can we create many macro variables using the BY VARIABLE... ?

 

I have tried something like this :

 

data test888;   
set pourcomptage ;  
	by cniv1_cniv3 ; 

	if first.cniv1_cniv3 then do ;
		call symputx("V1",col1); 
	end ;

	if last.cniv1_cniv3 then do ; 
		call symputx('V2',sumcum);    
	end ;
run;

The problem is that when i want to call the both macro variable, juste the last macro variable is stored.

 

These 2 macro variables permit me to calculate later the difference : %let nrep = %eval(&v1 - &v2) and then, on a SQL QUERY, calculate by hand, a percent by variable (cniv1_cniv3).

 

When I'm doing this :

 

%let nrep = %eval(&V1 - &V2) ;

Proc sql ;
create table indi_cm as 
select cniv1_cniv3, variable, (sum(count)/&nrep) as percent_mod
from nb_niv
group by cniv1_cniv3, variable ;
quit ;

I don't know how to say : "for the first cniv1_cniv3 you take the first &nrep associate to this cniv1_cniv3"

"for the second cniv1_cniv3, you take the second &nrep" .....

 

Do you have any idea ?

 

I don't put an example of data because I think that you can help me without !

 

Thank you in advance...

 

Onizuka

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

In general it is not a good idea to transfer values into macro variables just so you can later translate them back into values.

Keep the data in datasets.  Your code is not consistently referencing the same input dataset, but most likely you want to summarize the values 

data summary;    
   set pourcomptage ;  
   by cniv1_cniv3 ; 
   if first.cniv1_cniv3 then first_value=col1;
   if last.cniv1_cniv3 then do ; 
      last_value=sumcum;
      output;
   end;
   retain first_value;
   keep cniv1-cniv3 first_value last_value;
run;

and then merge/join the summary data back with the other data.

proc sql ;
create table indi_cm as 
  select a.cniv1_cniv3, a.variable
       , (sum(count)/(b.first_value - b.last_value) as percent_mod
  from nb_niv a
  left join summary b 
  on a.cniv1_cniv3 = b.cniv1_cniv3
  group by a.cniv1_cniv3, a.variable 
;
quit ;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

This is exactly what the macro engine is NOT for. Forget that macros exist at all (for the moment), you need to learn handling the Base SAS language first.

data test888;   
set pourcomptage ;  
by cniv1_cniv3 ; 
retain _col1;
if first.cniv1_cniv3 then _col1 = col1;
if last.cniv1_cniv3 then do ; 
  diff = sumcum - _col1; /* or whatever you want to calculate for the group */
  output;
end ;
run;
Tom
Super User Tom
Super User

In general it is not a good idea to transfer values into macro variables just so you can later translate them back into values.

Keep the data in datasets.  Your code is not consistently referencing the same input dataset, but most likely you want to summarize the values 

data summary;    
   set pourcomptage ;  
   by cniv1_cniv3 ; 
   if first.cniv1_cniv3 then first_value=col1;
   if last.cniv1_cniv3 then do ; 
      last_value=sumcum;
      output;
   end;
   retain first_value;
   keep cniv1-cniv3 first_value last_value;
run;

and then merge/join the summary data back with the other data.

proc sql ;
create table indi_cm as 
  select a.cniv1_cniv3, a.variable
       , (sum(count)/(b.first_value - b.last_value) as percent_mod
  from nb_niv a
  left join summary b 
  on a.cniv1_cniv3 = b.cniv1_cniv3
  group by a.cniv1_cniv3, a.variable 
;
quit ;
Onizuka
Pyrite | Level 9

Hello all,

 

Thank you a lot for all you responses, I think that the solution of @Tom is working perfectly and so as the solution of @Kurt_Bremser !

 

I don't know why I didn't think about doing a simple merge... !

 

I tried to do something similar, I did not think for a single second to the instruction "output"

Reeza
Super User
>I don't put an example of data because I think that you can help me without !

It's significantly easier to help you with it though.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 1018 views
  • 5 likes
  • 4 in conversation