BookmarkSubscribeRSS Feed
Sean_OConnor
Fluorite | Level 6

Folks,

 

I'm having a bit of trouble trying to understand why my code won't work. I've created a macro in order to carry out two different processes depending on the time period. 

 

Basically what I have is a datasource based of survey response and one based off administrative data. 

 

However, not all the individuals who are in the survey are actually in the administrative data set.

 

Therefore what I would like to do is stack on my missing individuals to my dataset to have a hybrid of survey values(when we can't find the person on the admin dataset) and admin values(when the person is on the admin dataset).

 

Depending on the year there is a need to call on different datasets. Therefore, I've created this macro.

 

However, I'm having issue getting it to run. Any input would be great. Perhaps I'm missing %end statment somwhere or something?

 

Any help would be great. 

 

%macro nonadmin;

%if year = 2011 %to 2013 %then %do;

data survey_&year(rename=(sex=sex1));
set mywork.combined_sample_&year_7;
run;

data admin_&year(keep=gender calc person_id sex agen);
set mywork.combined_sample_&year._7;
run;

proc sort data= survey_&year; by person_id;
proc sort data= admin_&year; by person_id;

data combine_&year;
set ssurvey_&year admin_&year;
by person_id;
if first.person_id and last.person_id then output
run;

%end;
 %else %if year = 2014 %to 2015 %then %do;

data survey_&year(rename=(sex=sex1));
set finaldat.survey&year;
run;

data admin_&year(keep=gender calc person_id sex agen);
set mywork.combined_sample_&year._7;
run;

proc sort data= survey_&year; by person_id;
proc sort data= admin_&year; by person_id;

data combine_&year;
set survey_&year admin_&year;
by person_id;
if first.person_id and last.person_id then output ;


run;
%end;

%mend nonadmin;
4 REPLIES 4
Kurt_Bremser
Super User
%if year = 2011 %to 2013 %then %do;

The text(!) "year" can never be equal to the text "2011".

That the %to in a condition does not throw an error baffles me, I must say. AFAIK, %to is only usable in a iterative %do statement.

 

You probably wanted something along the line

options minoperator;

%macro nonadmin(year) / mindelimiter=',';
%if &year in 2011,2012,2013 %then %do;

 Also see http://support.sas.com/kb/35/591.html

Sean_OConnor
Fluorite | Level 6

Kurt,

 

Do you mean something like this?

 

 

%macro nonadmin(year) / mindelimiter=',';
%if &year in 2011,2012,2013 %then %do;

data survey_&year(rename=(sex=sex1));
set mywork.combined_sample_&year_7;
run;

data admin_&year(keep=gender calc person_id sex agen);
set mywork.combined_sample_&year._7;
run;

proc sort data= survey_&year; by person_id;
proc sort data= admin_&year; by person_id;

data combine_&year;
set ssurvey_&year admin_&year;
by person_id;
if first.person_id and last.person_id then output
run;

%end;
 %else %if &year in 2014,2015 %then %do;

data survey_&year(rename=(sex=sex1));
set finaldat.survey&year;
run;

data admin_&year(keep=gender calc person_id sex agen);
set mywork.combined_sample_&year._7;
run;

proc sort data= survey_&year; by person_id;
proc sort data= admin_&year; by person_id;

data combine_&year;
set survey_&year admin_&year;
by person_id;
if first.person_id and last.person_id then output ;


run;
%end;

%mend nonadmin;

%nonadmin (2011);
%nonadmin (2012);
%nonadmin (2013);
%nonadmin (2014);
%nonadmin (2015);

When I run this I get the following error,

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       &year in 2011,2012,2013 
ERROR: The macro NONADMIN will stop executing.

Utilising Macro's are all fairly new to me but I appreciate any input. 

Kurt_Bremser
Super User

You missed to set the option I gave you:

*options minoperator;
%macro nonadmin(year) / mindelimiter=',';
%if &year in 2011,2012,2013 %then %do;
%put the value was found;
%end;
%mend;
%nonadmin(2011)

results in

24         *options minoperator;
25         %macro nonadmin(year) / mindelimiter=',';
26         %if &year in 2011,2012,2013 %then %do;
27         %put the value was found;
28         %end;
29         %mend;
30         %nonadmin(2011)
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       &year in 2011,2012,2013 
ERROR: The macro NONADMIN will stop executing.

but

options minoperator;
%macro nonadmin(year) / mindelimiter=',';
%if &year in 2011,2012,2013 %then %do;
%put the value was found;
%end;
%mend;
%nonadmin(2011)

results in

24         options minoperator;
25         %macro nonadmin(year) / mindelimiter=',';
26         %if &year in 2011,2012,2013 %then %do;
27         %put the value was found;
28         %end;
29         %mend;
30         %nonadmin(2011)
the value was found
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Would it not be simpler to put the data together, then do the processing?

data total_survey;
  set survey: indsname=tmp;
  year=scan(tmp,2,"_");
run;

data total_admin;
  set admin: indsname=tmp;
  year=scan(tmp,2,"_");
run;

Its just a matter of changing the set statements to be for your data.  The you have a total of each datasets to work with, avoids all the looping. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 743 views
  • 0 likes
  • 3 in conversation