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.
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
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
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
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
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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.