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

Hi folks,

How to create Variable ID and increment in Datastep or Proc sql?

I have a requirement to create a variable called id and that should have values as stated in the below example. Any help would be greatly appreciated. Also is there a way to make it generic by automating with a Macro or inside a macro?

ID

ABC.DEF.01

ABC.DEF.02

ABC.DEF.03

ABC.DEF.04

-.-----and so on.

or in other cases its like

ID

ABC.DEF.A1

ABC.DEF.A2

ABC.DEF.A3

...___  and so on

My thought is prolly to create 2 different columns, one with ABC.DEF and the other with 01.... and then concatenate them or maybe retain and increment?Well, I am not quite getting though.

1 ACCEPTED SOLUTION

Accepted Solutions
RichardinOz
Quartz | Level 8

For the second part

Data wantmore ;

     set have (firstobs = 100 obs = 343) ;

     Retain alpha 65 numero 0 ;

     numero + 1 ;

     ID = 'ABC.DEF.' || trim (byte (alpha)) || put (numero, 1.) ;

     if numero = 9 then

          do ;

               alpha + 1 ;

               numero = 0

          end ;

Run ;

Alert! untested code...

Richard

View solution in original post

4 REPLIES 4
RichardinOz
Quartz | Level 8

Are you aware that your first example will only provide 99 distinct ID values?  And your second only around 240?  Or if I interpret you correctly and both are to be part of the same series you will only get just over 340 values?

The first example is easy:

Data want ;

     Length ID $ 10 ;

     set have (obs = 99) ;

     ID = 'ABC.DEF.' || PUT (_N_, Z2.) ;

Run ;

Richard

RichardinOz
Quartz | Level 8

For the second part

Data wantmore ;

     set have (firstobs = 100 obs = 343) ;

     Retain alpha 65 numero 0 ;

     numero + 1 ;

     ID = 'ABC.DEF.' || trim (byte (alpha)) || put (numero, 1.) ;

     if numero = 9 then

          do ;

               alpha + 1 ;

               numero = 0

          end ;

Run ;

Alert! untested code...

Richard

RichardinOz
Quartz | Level 8

Oh, above code designed for Windows, should work on Unix;  but on zOS you will have to look up the EBCDIC collating sequence and deal with the split in the alphabet.

Richard

MarkWik
Quartz | Level 8

Hi Richard,

Thanks very much indeed. That was so helpful and well explained too. Cheers mate. I didn't know z2. format would give leading zeros.hmmmm!!!.

Thanks for taking the time to answer on a weekend,

Mark

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 4 replies
  • 6859 views
  • 3 likes
  • 2 in conversation