DATA Step, Macro, Functions and more

Initialize some variables of datasets

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Initialize some variables of datasets

Hi Everyone,

 

I want to initialize some variables of my datasets;

what i do is to create a macro variable contains the variable to initialize;

macro variable is like this  var1=.; var 2=""; ...etc.

In a data step i want to include this macro variable  by  &mv.;  to initialize this variables after a set statement, but put this error :"ERROR 180-322: Statement is not valid or it is used out of proper order."

So if sameone know how to do it !

 

Thanks.

 


Accepted Solutions
Solution
‎08-11-2017 06:11 AM
Regular Contributor
Posts: 240

Re: Initialize some variables of datasets

Hello,

 

As @RW9 said, you don't need macros to initialize some variables in a dataset.

 

data test;
infile datalines;
input x1 X2 X3 $;
cards;
1 2 3
4 5 6
7 8 9
run;

data inits;
	set test;
	/* create a new column */
	attrib x4 format=4. informat=4.;
	
	/* Reinitialize x2 - x3 */
	if _N_=2 then call missing(of x2--x3);
run;

View solution in original post


All Replies
Super User
Posts: 7,866

Re: Initialize some variables of datasets

Post code and log. Use the proper sub-windows ({i} and "little running man") for this.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 6

Re: Initialize some variables of datasets

Posted in reply to KurtBremser
data varexclu1; 
set varexclu; 
	if type eq "num" then varE = cat(compress(name)," =;");
	else varE = cat(compress(name),' =" " ;');
run; 
proc sql noprint; 
select varE into :vE separated by '  ' 
from varexclu1; 
quit; 

%put "&vE.";
data in; 
set out ;
		num 		  = 643;
		STAT	  = 6;
	        &vE.;
 run; 
Super User
Posts: 7,866

Re: Initialize some variables of datasets

If you use the call missing() subroutine in your created statements, you do not need to distinguish between character and numeric.

Otherwise, use a dot to set numerics to missing. X=; won't work.

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

Re: Initialize some variables of datasets

I would say your hitting Learn SAS 01 Problem, lack of understanding what Macro is and how it relates to Base SAS.  Base SAS is the programming language, it uses data, does manipulations etc.  Macro is not for this, it is purely a find replace/text generation utility.  Creation of variables should be done in Base SAS, within a datastep.  Not doing this will result in errors.  In all cases, macro is not needed, get code working, then if you see some repeating code, then it is the time to look at macro.  Without further information I cannot suggest.  Follow the guidance below Post button - post test data in the form of a datastep in a code window (its the {i} above post) and post what you want out.

Occasional Contributor
Posts: 6

Re: Initialize some variables of datasets

any solution ?
Super User
Super User
Posts: 7,997

Re: Initialize some variables of datasets

Give a valid scenario of where this would be used.  Provide test data in the form of a datastep, and what the ouptut should look like.  Why for instance would you want to create a whole set of empty varaibles - thats just a waste of disc space.  If you need to create something which matches an output specs, then create a template dataset:

proc sql;
  create DS_TEMPLATE
    (VAR1 char(200),
     VAR2 num);
quit;

Then set that with your data:

data want;
  set ds_template have;
run;

This will mean all variables in your template are in the output, blank where not present.

Solution
‎08-11-2017 06:11 AM
Regular Contributor
Posts: 240

Re: Initialize some variables of datasets

Hello,

 

As @RW9 said, you don't need macros to initialize some variables in a dataset.

 

data test;
infile datalines;
input x1 X2 X3 $;
cards;
1 2 3
4 5 6
7 8 9
run;

data inits;
	set test;
	/* create a new column */
	attrib x4 format=4. informat=4.;
	
	/* Reinitialize x2 - x3 */
	if _N_=2 then call missing(of x2--x3);
run;
Occasional Contributor
Posts: 6

Re: Initialize some variables of datasets

call missing' what's i'm searching for, thanks a lot @gamotte
☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 141 views
  • 0 likes
  • 4 in conversation