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

In the below data _null_ code:

1)How can I get to see the logic???I used the following options and still I cant find the logic in the log

options MPRINT MLOGIC SYMBOLGEN;

2)What is the meaning of:

call symputx(cats('id',_n_),sex);   /*especially what is the _n_ doing here*/

Regards

proc sort data=sashelp.class(keep=sex) out=class nodupkey;
by sex;

run;

data _null_;
   set class end=last ;
   call symputx(cats('id',_n_),sex);
   if last then call symputx('n',_n_);
   run;

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

Yes, it creates macro variables &id1,&id2,....;

example:

data have;
input sex$ ;
cards;
f
m
f
;
data _null_;
set have;
call symputx(cats('id',_n_),sex);
run;
%put &id1;
%put &id2;
%put &id3;

/* log file */

440  data have;
441  input sex$ ;
442  cards;

NOTE: The data set WORK.HAVE has 3 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


446  ;
447  data _null_;
448   set have;
449   call symputx(cats('id',_n_),sex);
450  run;

NOTE: There were 3 observations read from the data set WORK.HAVE.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


451  %put &id1;
f
452  %put &id2;
m
453  %put &id3;
f

View solution in original post

18 REPLIES 18
Linlin
Lapis Lazuli | Level 10

try this:

data class;

set sashelp.class;

postion_of_observation=_n_;

proc print;run;

robertrao
Quartz | Level 8

I tried and it (_n_)gives the observation number....

I still could not understand :

call symputx(cats('id',_n_),sex);

Please correct me:

it is creating macro variables id concatenated with observation number and ??????

Regards

data_null__
Jade | Level 19

Yes macro variables ID1 and ID2 with the values F and M respecfively.

Linlin
Lapis Lazuli | Level 10

Yes, it creates macro variables &id1,&id2,....;

example:

data have;
input sex$ ;
cards;
f
m
f
;
data _null_;
set have;
call symputx(cats('id',_n_),sex);
run;
%put &id1;
%put &id2;
%put &id3;

/* log file */

440  data have;
441  input sex$ ;
442  cards;

NOTE: The data set WORK.HAVE has 3 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


446  ;
447  data _null_;
448   set have;
449   call symputx(cats('id',_n_),sex);
450  run;

NOTE: There were 3 observations read from the data set WORK.HAVE.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


451  %put &id1;
f
452  %put &id2;
m
453  %put &id3;
f

robertrao
Quartz | Level 8

Thanks so much.

So far I was thinking only one macro variable can be created using call symput

But now I learnt that we can create as many variables as possible(for example if there are 10 observations under a particular variable we can create 10 macro variables......each macro variable getting the 10 value available under that variable????????

PLEASE CORRECT!!

DBailey
Lapis Lazuli | Level 10

You can create only one variable per statement.  That statement is being called for each record in the HAVE dataset.

Wesley
Fluorite | Level 6

Don't forget the last macro variable that's being created.  That would be "n".  The number of observations is being stored there.

Wes

Wesley
Fluorite | Level 6

Knowing how many macro variables are created will allow you to use a loop such as:

/*inside a macro of course*/

%do i = 1 %to &n;

  %put id&i = &&id&i;

%end;

Then you'll be able to see all the values that got assigned to the list of macro variables you created.  There are many other nice things you can do too.

robertrao
Quartz | Level 8

Thanks a lot for the detailed information.

Also in the below what is the use of knowing the the number of observations???

data _null_;
   set class end=last ;
   call symputx(cats('id',_n_),sex);
   if last then call symputx('n',_n_);
   run;

Also the macro and a do looop you explained gives us the list of macro variables created and their assigned values right???????

Thanks

Wesley
Fluorite | Level 6

Well, there is a lot of information in this.  ID1 has the value for the first observation (assuming you don't re-order the data set).  ID127 has the value for the 127th observation.  "n" tells us how many macro variables were created.  If you want to access all the variables created then you'll need this number.

Yes, this will reveal the values of all the macro variables you just created.

Typically, I'll group things together so I can process things differently.  I wouldn't use the method you're asking about.  I assume this is just an example of how to create many different macro variables at once.  I can't really see how to use that.  I can always do the following:

In the end you'll have as many different data sets as you have different sexes.  All the sets will combine to recreate the original data set.

/* the loop won't run in open code */

PROC SQL;

DROP TABLE sex_count;

CREATE TABLE sex_count as

select distinct sex

from class

;

quit;

%let number_of_sexes = &sqlobs;

%do i = 1 %to &number_of_sexes;

  DATA _NULL_;

  x=&i;

  SET sex_count point=x;

  CALL SYMPUT('CDE_SEX', sex);

  stop;

  run;

  %put &CDE_SEX;

  PROC SQL;

  DROP TABLE _.&CDE_SEX;

  CREATE TABLE _.&CDE_SEX as

  select *

  from class

  where SEX = "&CDE_SEX"

  ;

  quit;

%end;

Another method to use is:

PROC SQL noprint;

SELECT sex

  into :id1 - :id&n

from class

;

quit;

But you'll need to define "n" ahead of time.

Maybe I put too much in here.

robertrao
Quartz | Level 8

Thanks so much for your time...Its good to Know

Wesley
Fluorite | Level 6

Okay, I just re-read your original question.

The following will give you a data set in work that has only as many records as there are distinct values of sex.

proc sort data=sashelp.class(keep=sex) out=class nodupkey;
by sex;

run;

The following will create a macro variable for each unique value of sex.  If the only values are Male and Female then:

ID1 = Female

ID2 = Male

n = 2

Since this was sorted by sex (default is ascending) and F comes before M.

data _null_;
   set class end=last ;
   call symputx(cats('id',_n_),sex);
   if last then call symputx('n',_n_);
   run;

robertrao
Quartz | Level 8

Thanks so much Wesley for your time abd help

SOORISAS
Calcite | Level 5

Hi Robert,

Yes it is creating a macro variable Concatenating ID & the observation number and for the specific macro variable, it is assinging the value of SEX.

For example, See the result by using %put _user_ ;

GLOBAL ID10 M

GLOBAL ID1 M

GLOBAL ID11 F

GLOBAL ID2 F

GLOBAL ID12 F

GLOBAL ID3 F

GLOBAL ID13 F

GLOBAL ID4 F

GLOBAL ID14 F

GLOBAL ID5 M

GLOBAL ID15 M

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 18 replies
  • 3026 views
  • 6 likes
  • 8 in conversation