BookmarkSubscribeRSS Feed
pavelcik
Calcite | Level 5

Hi,

 

we have a huge problem with our code. We want to make a macro to categorize a dataset which looks like

variable with conditions   variable   group number    

var 1 <20                   variable name      1

20<var1<40               variable name      2 

40<var1                     variable name      3

so we would have a dataset like that:

var 1 <20                        1

20<var1<40                    2 

40<var1                          3

to assign the number, which is in grp column to each of conditions based on one variable.

unfortunately, the execution of the code ends with error:

 

WARNING: Apparent symbolic reference GRP_KAT not resolved.

 

NOTE: There were 3 observations read from the data set WYJ.VIN_KAT.

NOTE: The data set WYJ.VIN_KAT has 3 observations and 212 variables.

NOTE: Compressing data set WYJ.VIN_KAT increased size by 50.00 percent.

      Compressed is 3 pages; un-compressed would require 2 pages.

NOTE: DATA statement used (Total process time):

      real time           0.04 seconds

      cpu time            0.01 seconds

 

 

 

NOTE: Numeric values have been converted to character values at the places given by:

      (Line):(Column).

      697:187   697:196

ERROR: Symbolic variable name            . must begin with a letter or underscore.

NOTE: Invalid argument to function SYMPUT at line 697 column 180.

war=not missing(ACT_CALL_ACP) and ACT_CALL_ACP <= 1 zmienna=ACT_CALL_ACP grp=1 id=233 grp_kat=.

_ERROR_=1 _N_=1

NOTE: The SAS System stopped processing this step because of errors.

NOTE: There were 1 observations read from the data set WYJ.PODZIALY_INT_NIEM_ID.

      WHERE id=233;

WARNING: The data set WORK.TYMCZASOWE_OBIEGI may be incomplete.  When this step was stopped there

         were 1 observations and 5 variables.

NOTE: Compressing data set WORK.TYMCZASOWE_OBIEGI increased size by 100.00 percent.

      Compressed is 2 pages; un-compressed would require 1 pages.

WARNING: Data set WORK.TYMCZASOWE_OBIEGI was not replaced because this step was stopped.

NOTE: DATA statement used (Total process time):

      real time           0.03 seconds

      cpu time            0.01 seconds

 

 

 

%macro dane_kat;
%do i=1 %to 243;
	data tymczasowe_obiegi;
		set wyj.podzialy_int_niem_id  (where=(id=&i));
		call symput("war_kat", war);
		call symput("zmienna_kat", zmienna);
		call symput(grp_kat, grp);
	run;

	data &zb._kat;
		set &zb._kat;
		if &war_kat then put &zmienna_kat = &grp_kat;
	run;
%end;
%mend dane_kat;
%dane_kat;

Can you help us with that?

 


Zrzut ekranu 2016-05-11 o 19.32.44.pnghowitshouldbe.png
5 REPLIES 5
Astounding
PROC Star

There are many issues you will encounter before you come across a solution.  To get by the first error, note that you forgot the quotes:

 

call symput("grp_kat", grp);

 

I would recommend that you reduce the loops (go from 1 to 2, instead of 1 to 243).  Note what you hope the generated SAS program will look like, then compare that to what you actually get.  To be able to see what you actually get, add this at the top of your program:

 

options MPRINT;

 

But expect that getting this to work will be a multi-step debugging process and this is only the first step.

 

Good luck.

Reeza
Super User

So it sounds like you're trying to build rules for categorizing data. 

 

Proc Format is designed for this and has a structure that's flexible, maybe you can consider using that instead?

pavelcik
Calcite | Level 5

OK, but i would like to do something like that

proc format take the data from dataset with categorized variables by category 

var 1 a<2 grp=1

var 2 2<b grp=2

var 3 b<3 grp=3

and then combine it with other dataset that looks like:

var1 var2 var2 

and for example under var 2 there you've got var 1 a=0,5, so instead of

var1

0,5

the output dataset would be

var1

1

as the 0,5 fits into grp=1

 

is it possible to do that with proc format?

ballardw
Super User

I am guessing that what you are attempting to do is to use the example data set to write some that would look something like this:

data want;
   set new;
   if var <30 then var_cat = 1;
   if 30 le var < 50 then var_cat= 2;
   if 50 le var then var_cat = 3;

   if var2 <3 then var2_cat = 1;
   if 3 le var2 < 5 then var2_cat= 2;
   if 5 le var2 then var2_cat = 3;

/* repeat for multiple variables*/
run;

If so, then this may do some of what you want:

 

Replace "Have" with your starting data set and "Want" with a new output set.

data _null_;
   set wyj.podzialy_int_niem_id end=LastRecord; /* data set with conditions*/
   Length Lstr $ 200 tstr $ 32;
   If _n_ = 1 then call execute ("Data want; set have;");
   Else if LastRecord then call execute("run;");
   Else do;
      tstr = catt(zmienna,'_cat');
      Lstr = catx(' ','if',war,'then',tstr,'=',grp,';');
      call execute(lstr);
   end;
run;

You probably do not actually want to write into the Zmienna variable as you could change the value multiple times unless going to a lot of extra code to add ELSE into the IF lines that will be generated by Lstr.

 

If you want to see the code without running replace the Call Execute with PUTstatements. You write that to a file with a FILE statement in the Data _null_ and run that file after verifying it looks correct.

 

Also,

pavelcik
Calcite | Level 5

Can i use datasets in call execute?
like If _n_ = 1 then call execute ("wyj.wanteddata; wyj.podzialy_int_niem_id;");

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
  • 5 replies
  • 7105 views
  • 2 likes
  • 4 in conversation