BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

I've a macro variable which resolves as follows.

 

	%put &business_key;

CASHFLOW_DT|| '  |  ' || CASHFLOW_CD|| '  |  ' || NAME

If there is _DT in the value of the macro variable then either I need the macro variable to resolve as below or recreate another macro variable as shown below. _DT value can come at any position or sometimes it will not come as it depends on source

 

cats(put(input(CASHFLOW_DT, 8.), date9.)|| '  |  ' || CASHFLOW_CD|| '  |  ' || NAME

Thanks in advance

5 REPLIES 5
mkeintz
PROC Star

You are submitting the variable CASHFLOW_DT to an input function in the expression

   input(CASHFLOW_DT, 8.)

This means the CASHFLOW_DT is a character variable.  And the character value is a series of numeric characters representing the number of days after 01jan1960.  I doubt that is your situation.  

 

Please give some example of the values of the constituent variables, and what the corresponding business key would produce if executed.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

What is the CASHFLOW_DT variable?   

 

Is it a numeric variable that contains date values (number of days since 1960)?  If so then want PUT(CASHFLOW_DT,DATE9.) instead.

 

Is it a character variable that contains values that look like dates, like 2018-07-31 or 07/31/2018, then use the appropriate informat for the type of date strings it has.  PUT(input(CASHFLOW_DT,YYMMDD10.),DATE9.)

 

Is it a character variable that contains date value as a string of digits, like 21396 which would be '31JUL2018'd, then add an input() function call.  PUT(input(CASHFLOW_DT,32.),DATE9.)

 

Is it a numeric variable that contains numbers that look like dates in either YMD order, like 20180731, or MDY order like 07312018, or DMY order like 31072018 then you will need a triple sandwich to convert the number to a string, then to date and finally back to string. PUT(input(PUT(CASHFLOW_DT,z8.),YYMMDD8.),DATE9.)

 

If it is a number with date values and already has the DATE9. format attached to it and you are using the generated code in a data step then use the VVALUE() function.  VVALUE(CASHFLOW_DT).

 

Whatever syntax you need to generate just use the TRANWRD() function to convert your existing string to the new string.

%let new_business_key=%sysfunc(tranwrd(&business_key),VVALUE(CASHFLOW_DT),CASHFLOW_DT));
Astounding
PROC Star

I'm going to guess that you are merely trying to change the formula, but not computing anything yet.  Only when the macro variable gets used at a later point will you compute anything.  If that's right, then you could use:

 


data _null_;
if index("&business_key", '_DT') then call symput 

('business_key', "cats(put(input(CASHFLOW_DT, 8.), date9.)|| '  |  ' || CASHFLOW_CD|| '  |  ' || NAME" ) ;

run;

 

That replaces &business_key.  If you want a different macro variable instead, change the first parameter to CALL SYMPUT. 

 

 

s_lassen
Meteorite | Level 14

Sounds quite simple, just use a TRANWRD function:

%let business_key=CASHFLOW_DT|| '  |  ' || CASHFLOW_CD|| '  |  ' || NAME;
%put &business_key;
%let business_key=%sysfunc(tranwrd(&Business_key,CASHFLOW_DT,put(input(CASHFLOW_DT, 8.), date9.)));
%put &business_key;

Although I do not understand where the CATS function in your example comes in, am I missing something?

David_Billa
Rhodochrosite | Level 12
Instead of hardcoding like 'CASHFLOW_DT', can we do with wildcards like
%'_DT'%? Because it might be any variable which ends with _DT where I want
to implement this mechanism.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1083 views
  • 4 likes
  • 5 in conversation