BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
amager
Obsidian | Level 7

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

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

8 REPLIES 8
amager
Obsidian | Level 7
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; 
Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

amager
Obsidian | Level 7
any solution ?
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

gamotte
Rhodochrosite | Level 12

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;
amager
Obsidian | Level 7
call missing' what's i'm searching for, thanks a lot @gamotte

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 6682 views
  • 0 likes
  • 4 in conversation