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

Hello folks, I believe this is an easy one but somehow i can't figure out the macro to do it. I want to create 4 new datasets by specifying the value of a variable in an existing dataset. Below is my code which is very lengthy and redundant. 

data rcst1; set lcmf.cm20&fy1two.01a (keep=UCI RCAbrv Status); fy="&fy1two."; 
	where status in ('1','2'); run;

data rcst2; set lcmf.cm20&fy2two.01a (keep=UCI RCAbrv Status); fy="&fy2two."; 
	where status in ('1','2'); run;

data rcst3; set lcmf.cm20&fy3two.01a (keep=UCI RCAbrv Status); fy="&fy3two."; 
	where status in ('1','2'); run;

data rcst4; set lcmf.cm20&fy4two.01a (keep=UCI RCAbrv Status); fy="&fy4two."; 
	where status in ('1','2'); run;

*combine/append all 4 new dataset into one; data rcstall; set rcst1 rcst2 rcst3 rcst4; run;

Two things I wonder whether i can achieve:

1. since the existing macro starts from 1 to 4, can i create another loop to go from 1 to 4 instead of copying and pasting so many lines?

2. can I do one set command to append all four new datasets while adding a new variable "fy" to indicate the source of each dataset? 

 

Appreciate any feedback you may have. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

 

data rcstall;
	set lcmf.cm20&fy1two.01a (keep=UCI RCAbrv Status)
              lcmf.cm20&fy2two.01a (keep=UCI RCAbrv Status)
              lcmf.cm20&fy3two.01a (keep=UCI RCAbrv Status)
              lcmf.cm20&fy4two.01a (keep=UCI RCAbrv Status)
                    INDSNAME = src;

*can use text operations to parse this to desired format;
fy = src;

where status in ('1', '2');

run;

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User
This could possibly be simplified further, what does fy1two to fy4two look like.
The INDSNAME option allows you to identify the input data set names.
Reeza
Super User

 

data rcstall;
	set lcmf.cm20&fy1two.01a (keep=UCI RCAbrv Status)
              lcmf.cm20&fy2two.01a (keep=UCI RCAbrv Status)
              lcmf.cm20&fy3two.01a (keep=UCI RCAbrv Status)
              lcmf.cm20&fy4two.01a (keep=UCI RCAbrv Status)
                    INDSNAME = src;

*can use text operations to parse this to desired format;
fy = src;

where status in ('1', '2');

run;

 

 

kevsma
Quartz | Level 8

Thanks @Reeza this works perfectly! 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 154 views
  • 0 likes
  • 2 in conversation