BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
adamqureshi_21
Calcite | Level 5

Hello!

 

I have a dataset that contains the variable record_id. I am using the substr() function to filter by the first two numbers in this record_id variable and using a proc freq to create some tables. Example shown below: 

 

proc freq data = import;
tables sex ethnicity age;
where substr(record_id,1,2)='14';
run;

What I would like to do is create a macro that changes the value of the substring to numbers between 13 - 19, as that encompasses all the prefixes for the record ID's. I'm completely inexperienced with macros so I'm not sure how to go about doing this, and I'm not sure whether to create a macro that encompasses the whole proc freq statement, or just the value of the substring. Any tips or help is appreciated. 

 

Thanks!

Best,

Adam

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

How about this:

 

%macro dothis(string=);
proc freq data = import;
tables sex ethnicity age;
where substr(record_id,1,2)="&string";
run;
%mend;
--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

How about this:

 

%macro dothis(string=);
proc freq data = import;
tables sex ethnicity age;
where substr(record_id,1,2)="&string";
run;
%mend;
--
Paige Miller
adamqureshi_21
Calcite | Level 5
That worked perfectly! It looks like I was just overthinking this. Thank you so much!

Best,
Adam
PaigeMiller
Diamond | Level 26

@adamqureshi_21 

 

So, now I have to comment, I think you should do this without macros.

 

Example:

 

data intermediate;
    set import;
    prefix=substr(record_id,1,2);
run;
proc freq data=intermediate;
    tables prefix*(sex ethnicity age);
run;
--
Paige Miller
adamqureshi_21
Calcite | Level 5
Ah I see, that seems arguably easier. Thank you for this!
ballardw
Super User

Even shorter which takes advantage only wanting the first two characters. The format applied will have the output show just the first two characters. No need for additional variables or data set.

proc freq data=import;
    tables record_id*(sex ethnicity age);
    format record_id $2. ;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 743 views
  • 1 like
  • 3 in conversation