BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hellohere
Pyrite | Level 9

I am getting macro variables and need write-out their values into file line-by-line. 

Somehow, confused into passing the value into dataset. 

Totally alike code is below. 

 

	data _temp;
	set sashelp.class;
	ind=_N_;
	run;quit;
	proc sql noprint;
	select age into: val_age 	from _temp having ind=max(ind); 
	select height into: val_height 	from _temp having ind=max(ind); 
	select weight into: val_weight 	from _temp having ind=max(ind); 
	quit;
	%put "val_age=&val_age.";

	data _out_c;
	input line $64.;
	datalines;
	[value]
	age="&val_age."
	height=&val_height.
	weight=&val_weight.
	run;quit;

Tried various combinations, it does not show up values in dataset _out_c. Anyone?!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Why do you want to do this? Seems to be part of a bigger problem? In that case, describe your actual problem. There is probably a better way than this 🙂

 

If your question is merely related to using macro variables in cards/datalines, read Example 12: Using Macro Variables within a CARDS or DATALINES Statement.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Why do you want to do this? Seems to be part of a bigger problem? In that case, describe your actual problem. There is probably a better way than this 🙂

 

If your question is merely related to using macro variables in cards/datalines, read Example 12: Using Macro Variables within a CARDS or DATALINES Statement.

Tom
Super User Tom
Super User

@hellohere wrote:

I am getting macro variables and need write-out their values into file line-by-line. 

Somehow, confused into passing the value into dataset. 

Totally alike code is below. 

 

	data _temp;
	set sashelp.class;
	ind=_N_;
	run;quit;
	proc sql noprint;
	select age into: val_age 	from _temp having ind=max(ind); 
	select height into: val_height 	from _temp having ind=max(ind); 
	select weight into: val_weight 	from _temp having ind=max(ind); 
	quit;
	%put "val_age=&val_age.";

	data _out_c;
	input line $64.;
	datalines;
	[value]
	age="&val_age."
	height=&val_height.
	weight=&val_weight.
	run;quit;

Tried various combinations, it does not show up values in dataset _out_c. Anyone?!


You have to put assignment statements into the CODE of the data step.  Not into any in-line source data text.

data _out_c;
  age="&val_age.";
  height=&val_height.;
  weight=&val_weight.;
run;

If you want to WRITE values then just use the macro variables to generate the PUT statement to WRITE the lines.

data _null_;
  file 'name_of_file_you_want_to_write' ;
  put  "age=""&val_age."""
     / "height=&val_height."
     / "weight=&val_weight."
  ;
run;

Also why are you putting quotes around the AGE value?  Is AGE a character variable?

Also your SQL is inefficient.

proc sql noprint;
select age ,height ,weight
  into :val_age 	
     , :val_height
     , :val_weight
from _temp
having ind=max(ind)
; 
quit;

 

Kurt_Bremser
Super User

First of all, DATALINES and macro don't go together. Macro triggers in DATALINES are not resolved, and you cannot use DATALINES in a macro.

DATALINES is always the last statement in a data step and constitutes a step boundary. Therefore, no RUN is needed, and any data step code after it causes a syntax error.

 

It looks as if you want to use values from the last observation of one dataset in code creating another dataset.

To retrieve a value from the last observation quickly, do this:

data _null_;
set sashelp.class nobs=nobs point=nobs;
call symputx("val_age",age);
stop;
run;

How you need to use the macro variable depends on the code where it should be used, so please show us that.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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
  • 644 views
  • 2 likes
  • 4 in conversation