DATA Step, Macro, Functions and more

Need help to concatenate values and keep in macro variables to get user defined format

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Need help to concatenate values and keep in macro variables to get user defined format

I have below data in X dataset;

 

BRAND           PRICE

----------          --------------

Apple             50000

Samsung       20000

MI                  15000

LG                35000

 

Now I need a below values in 'get' macro variable.

 

get value:

============

Apple(50000),Samsung(20000),MI(15000),LG(35000).

 

Please suggest me which one is a easiest way to do above. Thanks in advance.


Accepted Solutions
Solution
‎04-27-2017 09:08 AM
PROC Star
Posts: 734

Re: Need help to concatenate values and keep in macro variables to get user defined format

[ Edited ]

Like this?

 

data X;
input BRAND $ PRICE;
datalines;
Apple 50000
Samsung 20000
MI 15000
LG 35000
;

data help(keep=var);
	set X;
	var = cats(BRAND,'(',PRICE,')');
run;

proc sql noprint;
	select var INTO: get separated BY ','
	from help;
quit;

%put &get;

View solution in original post


All Replies
PROC Star
Posts: 734

Re: Need help to concatenate values and keep in macro variables to get user defined format

So you want get_value to be a macro variable containing the value Apple(50000),Samsung(20000),MI(15000),LG(35000)?

Contributor
Posts: 36

Re: Need help to concatenate values and keep in macro variables to get user defined format

Yes. Need complete value in macro varaible.

Solution
‎04-27-2017 09:08 AM
PROC Star
Posts: 734

Re: Need help to concatenate values and keep in macro variables to get user defined format

[ Edited ]

Like this?

 

data X;
input BRAND $ PRICE;
datalines;
Apple 50000
Samsung 20000
MI 15000
LG 35000
;

data help(keep=var);
	set X;
	var = cats(BRAND,'(',PRICE,')');
run;

proc sql noprint;
	select var INTO: get separated BY ','
	from help;
quit;

%put &get;
Contributor
Posts: 36

Re: Need help to concatenate values and keep in macro variables to get user defined format

Great. It's working great, Thank you.

 

Now I want to keep this value in email and am using below. But macro variable is not working.

Can you sugegst me why?

 

x %tslit(Total sales are as follows: &get."

| mailx -s "Report" xxxxxx@xxx.com);

 

I am getting mail woth out resolving macro varibale.

 

Contributor
Posts: 36

Re: Need help to concatenate values and keep in macro variables to get user defined format

Sorry..Please find correct code below.

 

x %tslit(echo "Total sales are as follows: &get."

| mailx -s "Report" xxxxxx@xxx.com);

PROC Star
Posts: 734

Re: Need help to concatenate values and keep in macro variables to get user defined format

I dont understand your question, please elaborate.

Contributor
Posts: 36

Re: Need help to concatenate values and keep in macro variables to get user defined format

Initially we keep the value in macro variable,now needs to include that macro variable  value in email using X coomand.

In that &get value is not resolving in X command. Please see my code in previous post.

Super User
Super User
Posts: 7,942

Re: Need help to concatenate values and keep in macro variables to get user defined format

X command is for invoking Operating System commands - nothing to do with SAS - so do you have an Operating System command:

%tslit

Unlikely, that looks like a SAS macro call.

Another problem, per my previous message, when your data gets large you will break then subject line limits.  A good way to data into an email is to put it out from a dataset:

filename mailbox email 'someone@world.com'
  subject='Total Sales';
data _null_;
  file mailbox;
  set your_data;
  if _n_=1 then do;
    put "Hello";
    put "This is a the Total Sales";
  end;
  put _all_;
run;
filename mailbox clear;

You can find further examples, such as attaching reports, here:

http://www2.sas.com/proceedings/forum2008/038-2008.pdf

Contributor
Posts: 36

Re: Need help to concatenate values and keep in macro variables to get user defined format

I tried this way.but getting an error like connection was refsued. Don't have proper setup to execute above one.

Using X command other macro variables are resolving,but above macro variable is not resolving.it might due to large value or some other reason. Thanks.

Super User
Posts: 7,762

Re: Need help to concatenate values and keep in macro variables to get user defined format

When you get a connection refused, your mail setup is not working at all. Verify that you can send a simple mail before you embark in more complex things.

 

Also provide the complete code of the macro %tslit, so we can get a feeling for what it's supposed to do. Note that your X statement will finish before you start sending mail.

 

But make sure that you can send mail from SAS first.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,942

Re: Need help to concatenate values and keep in macro variables to get user defined format

Well, as always, my first question will be why?  Macro is a text find and replace tool, it is not for data processing, that is what datasteps are for.  If you go down that route you will effectively double the complexity of your code and obfuscate it to a certain degree.  Also you will likely hit the boundaries of lengths of variables, and certain characters within there.  So I wouldn't recommed this approach.  There are numerous methods of working with data with are both more effective and simpler to work with.  Provide some background and will be able to suggest.

☑ This topic is solved.

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

Discussion stats
  • 11 replies
  • 175 views
  • 3 likes
  • 4 in conversation