BookmarkSubscribeRSS Feed
Stat_prevmed
Fluorite | Level 6

I have the following data structure

 

ID Med1_1-Med1_5 Med_oth1  Med2_1-Med2_5 Med_oth2 Med3_1-Med3_5 Med_oth3 ....   Med20_1-Med20_5 Med_oth20 

 

I need to do the same thing to the  variable in each group of 6 variables "MedX_1-MedX_5 Med_othX" .  Basically find the one that is not null and assign that to a new variable. 

I have done this before and I cannot find my example. The following is my attempt to do this with a macro and it is not working. Any help would be much appreciated 

 


%macro Choose_med;

%do i = 1 %to 20;

%if Med&i._1 = 1 %then Med&i = Med_Ot&i ;
%else %if Med&i._1 ^= 1 or Med&i._1=. %then %do j = 1 %to 5;

%if Med&i._&j ^= 1 and Med&i._1^=. and Med&i="" %then Med&i = Med&i._j ;
%end;

%end;
%mend Choose_med;

Data Meds2;
set meds;
format Med1-med20 $100.;
%Choose_med;
run;

 

 

3 REPLIES 3
Astounding
PROC Star

Macro language does not process the data in your data set.  It is not capable of doing that.  Whatever you did before either won't be helpful here, or it didn't use macro language.

 

This is likely an application for arrays.  To get some help, you will need to type out in full the logic you want to apply to one group of variables, say Med1_1 - Med1_5.  If you do that, lots of people will be able to help with the solution.

PaigeMiller
Diamond | Level 26

Run the code again, using this command as the first command of your code:

 

options mprint;

Then examine the log to see what the problem is. If that doesn't clear things up, show us the log by pasting it into the window that appears when you click on the {i} icon. DO NOT SKIP THIS STEP.

 

Also, you say: "it is not working". This is never enough information for us. Please describe in words and in detail (and show us if possible) the actual information that leads you to say it is not working.

--
Paige Miller
Tom
Super User Tom
Super User

Your approach is confusing macro logic and SAS code logic.  For example the condition in the your first %IF can never be true for any value of &I since the letter M is never going to match the digit 1.

Med&i._1 = 1 

If you want more help so a simple example of what variables you have and what new variables you want to create based on the values of those variables.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 811 views
  • 0 likes
  • 4 in conversation