New variable with values based on IF structure with arguments from the rest of the data set

Accepted Solution Solved
Reply
New Contributor e11
New Contributor
Posts: 2
Accepted Solution

New variable with values based on IF structure with arguments from the rest of the data set

I have the following challenge: I have a data set called defaults with variables “status”, “default_year” and “signature_year”. I want to create a new data set with the old data set as it is and a new variable “default_in_Yx”. The new variable should be conditional on the values of the other variables for each observation. The following IF structure is what I need:

 

 Default_in_Yx=ifc(status="DEFAULTED", ifc(default_year=signature_year+0, "Y0",  ifc(default_year=signature_year+1,"Y1", "" )),"")

 

So if the default_year=signature_year the value in the new variable should be Y0, if default_year=signature_year+1 it should be Y1 and so on, and I need a code that produces this for any values of default_year and signature_year.

 

I have tried my way forward and the closest thing to a solution may be the following, however it does not work.

 

%macro def_year;
%let e=0;
%let Yx=%sysfunc(IFC(default_year=signature_year+&e,"Y&e",""));
	%do %until ("&Yx" NE "");
		IFC(default_year=signature_year+&e,"Y&e","");
			%let e=%eval(&e+1);
		%let Yx=%sysfunc(IFC(default_year=signature_year+&e,"Y&e",""));
	%end;
%mend;

%let YxValue=%def_year;

data newtable;
set work.defaults;
Default_in_Yx=ifc(transaction_status="DEFAULTED",&YxValue,"");
run;

 

Any help and suggestions would be very appreciated.

Thanks, Erik 


Accepted Solutions
Solution
‎08-19-2016 10:16 AM
New Contributor e11
New Contributor
Posts: 2

Re: New variable with values based on IF structure with arguments from the rest of the data set

Thanks for your replies. I am relatively new to SAS and coding and after some more thoughts I found an incredibly easier solution using concatenation:

 

data newtable;

set work.defaults;

Default_in_Yx=ifc(status="DEFAULTED","Y"||strip(default_year-signature_year),"");

run;

 

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,232

Re: New variable with values based on IF structure with arguments from the rest of the data set

Hi,

 

Could you post some test data (in the form of a datastep) follow this link if necessary:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

And what the output should look like. I would suspect looking at what you have posted, using arrays would be easier.

Esteemed Advisor
Posts: 6,684

Re: New variable with values based on IF structure with arguments from the rest of the data set

Severe misunderstanding of the purpose of the macro language.

 

DO NOT try to create values with macro language. The macro language is a text generating language for creating dynamic program code.

It also knows only one datatype, text!

When you execute your macro, the ifc() function is fed the text value "default_year=signature_year+0" as a condition. In fact, the macro engine recognizes "default year" to not be a macro variable, and therefore puts implicit quotes around it to make it text before feeding to the ifc() function; the same happens to "signature_year". So the condition ("default_year" = "signature_year+0") can never be valid.

If you want to create a macro variable by using data step functions, do so in a data _null_ step, and use call symput() at the end to store your result in a macro variable.

 

PS

This part

	%do %until ("&Yx" NE "");
		IFC(default_year=signature_year+&e,"Y&e","");
			%let e=%eval(&e+1);
		%let Yx=%sysfunc(IFC(default_year=signature_year+&e,"Y&e",""));
	%end;

would repeatedly feed the program line

IFC(default_year=signature_year+&e,"Y&e","")

 to the SAS interpreter where the macro is resolved, probably causing lots of error messages.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎08-19-2016 10:16 AM
New Contributor e11
New Contributor
Posts: 2

Re: New variable with values based on IF structure with arguments from the rest of the data set

Thanks for your replies. I am relatively new to SAS and coding and after some more thoughts I found an incredibly easier solution using concatenation:

 

data newtable;

set work.defaults;

Default_in_Yx=ifc(status="DEFAULTED","Y"||strip(default_year-signature_year),"");

run;

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 244 views
  • 4 likes
  • 3 in conversation