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

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

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

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.


Accepted Solutions
Solution
‎05-04-2013 06:50 AM
Super Contributor
Posts: 644

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

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


All Replies
Super Contributor
Posts: 644

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

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

Solution
‎05-04-2013 06:50 AM
Super Contributor
Posts: 644

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

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

Super Contributor
Posts: 644

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

Posted in reply to RichardinOz

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

Frequent Contributor
Posts: 75

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

Posted in reply to RichardinOz

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 1700 views
  • 3 likes
  • 2 in conversation