Do Loop through a unknow number of value i and variable name

Reply
Super Contributor
Posts: 371

Do Loop through a unknow number of value i and variable name

[ Edited ]

Hi Everyone,

 

I have a data with a Target variable and a number of condition variable ( a b c d e f). 

For each combination of 2 condition value (a=1, b=2 OR a=1, b=1...). I want to count the number of record for each Target Value
under CERTAIN condition. (I will extend it to 3 conditions in the future)

Because of the conditions I will impose to process each subsample, I need to
structure the code as below.

 

I think my issue is to get SAS automatically know the number of discrete value for each variable and let it run through them;

 

Thank you for your help.

HHC

 

 

data have; 
input target a b c d e;
cards;
1 1 2 3 0 6
2 1 2 0 0 6
-1 1 2 0 8 16
-2 2 0 0 8 16
5 2 0 0 8 31
6 0 1 2 2 31
run;



*pull record for a given condition ---
*this one later will be built in a Do loop &var1=a &va1_value=1 &var2=b var2_value=2; data temp1; set have; if a=1 and b=2; run;
*section to add condtion to process the data; *section to add condtion to process the data;
*summary the number of record-------------------------------------------; proc freq data=temp1; table target/out=temp2 (drop = PERCENT);run;
*standardizethe report so it can be added to a master file--------------; proc transpose data=temp2 out=temp3 (drop =_LABEL_) prefix=target_value; id target; run; data report; set temp3; var1="a"; var1_value=1; var2="b"; var2_value=2;run; *So at that step, I am done with the first combination of 2 factors; *I need a do loop to go over all variable and all value available for each factor; *I think 1 issue is to get SAS automatically know the number of discrete value for each variable
and let it run through them;

 

 

Super User
Posts: 17,784

Re: Do Loop through a unknow number of value i and variable name

Sounds a bit like market basket analysis. Have you tried any of the MBA macros?

Super Contributor
Posts: 371

Re: Do Loop through a unknow number of value i and variable name

[ Edited ]

After thinking more about it, I think the following framework below could potentially ok.

But how I can get SAS know the list of dicrete value for each variable.

 

(I am not familiar with MBA macros)

 


%let VARIABLE_NAME= A B C D E;
%let variable_value
do i=1 to dim(VARIABLE_NAME);
  do j=i+1 to dim(VARIABLE_NAME);	* 
  	do k=1 to dim(variable_value);
		do l=1 to dim(variable_value)

		data temp; set have;  *Get the data needed;
		if VARIABLE_NAME(i)=variable_value(k) and VARIABLE_NAME(j)=variable_value(l);
		run;

		*** run the middle code***;
end;
end;	
end;
end;

 

Super User
Posts: 17,784

Re: Do Loop through a unknow number of value i and variable name

Your mixing SAS base code and macro code...you'll need macro variables instead and %do loops. You can get the numbers by running a proc freq on your dataset and creating macro variables from the results.

Super Contributor
Posts: 371

Re: Do Loop through a unknow number of value i and variable name

So the code below will generate the distint value for each variables.

I am not sure how to take information from this table to work with the original table.

 


data have; 
input target a b c d e;
cards;
1 1 2 3 0 6
2 1 2 0 0 6
-1 1 2 0 8 16
-2 2 0 0 8 16
5 2 0 0 8 31
6 0 1 2 2 31
run;

proc summary data=have missing chartype; 
   class _all_;
   ways 1; 
   output out=distinct(drop=_type_ _freq_) / levels; 
   run; 

proc sort data=distinct;
   by _level_;
   run; 
data Variable_value;
   update distinct(obs=0) distinct;
   by _level_;
   run; 
 
Ask a Question
Discussion stats
  • 4 replies
  • 349 views
  • 0 likes
  • 2 in conversation