BookmarkSubscribeRSS Feed
jmmedina25
Obsidian | Level 7

Hi!  I have 4 data sets, data1-date4.  I need a referral_date variable for each data set that corresponds to the dataset name.

 

For instance, in data1 referral_date=January2020 and in data2 referral_date=February2020.

 

I would like to know how to do this in a macro since creating a variable for each dataset individually takes up time and space. Thanks!

3 REPLIES 3
ballardw
Super User

You don't need a macro just a data step. The INDSNAME option on a Set statement indicates the current data set a current record comes from. So:

 

data example;
   length reference_name $ 32.;
   set sashelp.class indsname=tempname;
   reference_name = scan(tempname,2);
run;

The reference_name variable will have the value CLASS for each record as that is the name. If you want the library as well make the reference name longer (max 41) and don't use scan.

 

This will work for multiple sets so if there are multiple data sets on the SET statement the value of Reference_name changes with the changing data set source.

Kurt_Bremser
Super User

Since adding a variable requires a rewrite of the dataset, you won't get around the time it takes to read and write the whole dataset. Why don't you add the variable later, when processing these datasets?

Reeza
Super User

@jmmedina25 wrote:

...

 

I would like to know how to do this in a macro since creating a variable for each dataset individually takes up time and space. Thanks!


Assuming your dates are hardcoded so that the first date is Jan2020.

data _null_;
date_start = '01Jan2020';
do i=1 to 4;
call symputx(
    catt( 'referral_date', put(i, 2. -l) ), 
    catt( put(date_start, monname12.), put(date_start, year4.) )
    );


date_start = intnx('month', date_start, 1, 'b');
end;

run;

%put referral_date1.;
%put referral_date2;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 809 views
  • 0 likes
  • 4 in conversation