BookmarkSubscribeRSS Feed
hhchenfx
Barite | Level 11

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;

 

 

4 REPLIES 4
Reeza
Super User

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

hhchenfx
Barite | Level 11

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;

 

Reeza
Super User

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.

hhchenfx
Barite | Level 11

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; 
 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 794 views
  • 0 likes
  • 2 in conversation