BookmarkSubscribeRSS Feed
Bal23
Lapis Lazuli | Level 10

@Reeza

 

would you please show me how to use "indsname" given the sample dataset I have provided? Thanks

 

I do not know how to list variables there, so please provide sas sample code

Reeza
Super User

INDSNAME does not list variables, it creates a variable that shows where a row came from. You're also mixing solutions, if you want a macro you don't need INDSNAME, follow the steps I outlined.  If you want a single proc then you would use the INDSNAME option.

 

*Generate sample data;
data class;
set sashelp.class(obs=5);
run;

data class2;
set sashelp.class(obs=13 firstobs=9);
run;

/*Sample using INDSNAME*/
data example;
set class class2 indsname=source;
data_source = source;
run;

proc print data=example;
run;

OUTPUT:

 

  Obs    Name       Sex    Age    Height    Weight    data_source

                  1    Alfred      M      14     69.0      112.5    WORK.CLASS
                  2    Alice       F      13     56.5       84.0    WORK.CLASS
                  3    Barbara     F      13     65.3       98.0    WORK.CLASS
                  4    Carol       F      14     62.8      102.5    WORK.CLASS
                  5    Henry       M      14     63.5      102.5    WORK.CLASS
                  6    Jeffrey     M      13     62.5       84.0    WORK.CLASS2
                  7    John        M      12     59.0       99.5    WORK.CLASS2
                  8    Joyce       F      11     51.3       50.5    WORK.CLASS2
                  9    Judy        F      14     64.3       90.0    WORK.CLASS2
                 10    Louise      F      12     56.3       77.0    WORK.CLASS2

 

Bal23
Lapis Lazuli | Level 10

Thanks. I still do not get where "source", from your code below, is from, and how I should replace it. I understand there are two options, one is macro code, that I will be working on, and this solution as well.

 

I do not know what 'source' means in your code, this is why I provided my sample dataset.

 

I do not understand how to use "indsname" and I do not know where  'source' is from. that is hard for me to learn. Would you please give me a sas code, based on my sample dataset, so that I can understand how to use "indsname"

indsname

 

LinusH
Tourmaline | Level 20
Wrap your code within a macro definition. The macro needs a parameter, the data sat name.
The parameter acts as macro variable within the macro, so just replace the data set names in your code with that.
Then call the macro once for each data set you wish to use.
See syntax and examples in the on line documentation.
Data never sleeps
Bal23
Lapis Lazuli | Level 10

 Thank you. Would you please provide sample code? That will be very helpful. It is hard for me to understand.

Reeza
Super User
  1. Wrap your code in %macro/%mend and assign a name, eg:
  2. %macro summarize;
    
    *your sas code;
    
    %mend;
  3. Test that it works by calling it. 
    %summarize;
  4. Change macro to use a parameter  
    %macro summarize(dataset);
  5. Change variable name in code to be parameter value, eg:
    proc means data=&dataset;
    ...
    run;
  6. Test again 
    %summarize(bpchild);
  7. Make sure it works for multiple cases.
Bal23
Lapis Lazuli | Level 10

@Reeza

 

this is my code, based on your code above

I am thinking to have a variable that can be used to replace child, adult, senior and over100. There are some problems with this code, so please give advice. thanks

 

%macro sum;

  proc sort data=bp2∑
by patientid;
run;
 PROC means DATA=bp2∑
var wvr_:;
run;

    %mend;
	
data want;
set bp2∑
run;
%sum(child, adult, senior, over100)

  

    
RW9
Diamond | Level 26 RW9
Diamond | Level 26

So from the test data you posted:

data childhave;
input patientid var_hed var_eye var_col var_leg var_hea var_lea var_alh;

You proc means is simply:

 

proc means data=childhave;
  var var_:;
run;

 

 

Bal23
Lapis Lazuli | Level 10

that is right

but in order to avoid to replace my dataset repeatedly, i am asking for advice to have a sas macro, so that I need not do it repeatedly

 

also, i am asking for advice whether there is a sas code to compare the frequency of each variable afterwards

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, why do you need to replace your dataset repeatedly?  You posted some test data, I posted a proc means, which creates one proc means output for the data you provided.  Where oes this repetition come into it?

Bal23
Lapis Lazuli | Level 10

That is my question. I have four simiar datasets. I need to generate four tables, regarding the frequency order of these variables. I provided one sample dataset. My question is how to use macro so that  i need not replace them repeatedly.

 

please see my original post below

 

I have four datasets, child, adult, senior and over100, each one has over 10000 obs.

the orginal code is very simple

 

proc sort data=bpchild;
by id;
run;
PROC means DATA= bpchild;
var wvr_:;
run;
proc contents data= bpchild;
run;

 

 I just need to replace child with adult, senior, over100.

would anybody teach me how to use macro do this?

Reeza
Super User

Post your question about comparing freq as a new thread please, this one is confusing enough as it is.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

So back to the code posted on the first page:

/* Put the four datasets together and create a variable which idenfies which dataset each observation came from */
data all;
  set child adult senior over100 indsname=source;
  dsetname=source;
run;
proc sort data=all;
  by dsetname id;
run;

/* Run a proc means on this one dataset, do it by dateset the original observation came from to split the results on dataset */
proc means data=all;
  by dsetname;
  var var_:;
  output out=want n=n mean=mean...;
run;

/* Run a proc freq on this one dataset, do it by dataset the original observation came from */
proc freq data=all;
by dsetname;
...
run;
Bal23
Lapis Lazuli | Level 10

@RW9

Thank you. Can you explain your last part?

/* Run a proc freq on this one dataset, do it by dataset the original observation came from */
proc freq data=all;
by dsetname;
...
run;

I do not know what to add, I add "patientid" or not add, sas is busy.

 

what do you mean by

do it by dataset the original observation came from 
Bal23
Lapis Lazuli | Level 10

@RW9

Thank you.

I did run your code. The problem is, I could not generate these four tables that I expect.

what should I do with the dataset "want"? I need to count the frequencies of those variables.

 

var_hed var_eye var_col var_leg var_hea var_lea var_alh

 would you plesae explain a little more about your code below regarding those  frequencies variables that I need to count and I need to list them according to order of the frequencies.

proc means data=all;
  by dsetname;
  var var_:;
  output out=want n=n mean=mean...;
run;

/* Run a proc freq on this one dataset, do it by dataset the original observation came from */
proc freq data=all;
by dsetname;
...
run;

 

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