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

The below codes is working fine.  Just want to know if there is any way to have to same result without RENAME and DROP variables? Thanks.

 

DATA A;
INFILE CARDS;
INPUT ID ;
CARDS;
3453
123162
124171
1241761
;
RUN;

DATA A(RENAME=(ID1=ID));
SET A;
ID1 = PUT(ID,z7.);
DROP ID;
RUN;

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

In your output ID1 is numeric while ID is char type.

Your code is the easyiest way to bo done;

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18

In your output ID1 is numeric while ID is char type.

Your code is the easyiest way to bo done;

PGStats
Opal | Level 21

Do it in a single step:

 

DATA A;
INFILE CARDS;
INPUT ID :$7. ;
id = translate(right(id),"0"," ");
CARDS;
3453
123162
124171
1241761
; 

 

PG
Shmuel
Garnet | Level 18

@PGStats, you probably know that input a CHAR type is left aligned.

Then 

DATA A;
INFILE CARDS;
INPUT ID :$7. ;
id = translate(right(id),"0"," ");
CARDS;
3453
; run;

will create ID='3453000' while the equivalent to the number 3453 is '0003453';

 

In this case, it can be done by:

DATA A;
INFILE CARDS;
INPUT ID :$7. ;
id = put(input(id,best7.),z7.);
CARDS;
3453
; run;

 

PGStats
Opal | Level 21

Hi @Shmuel, you are right in saying that input reads left aligned, which is why I use the right function to make it right aligned before replacing the spaces with leading zeros.

PG
Shmuel
Garnet | Level 18

Hi @PGStats, you are right, I skiped the right in your code.

 

Any way, our debate is a good lesson to SAS starting users.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry @Shmuel that may not be 100% correct.  It depends on what the OP user is expecting from the variable.  If they really do want a text field then I have no problem with it.  If however they just want that data as z7. then the simplest method is to apply the format to the already existing numeric data:

data a;
  infile cards;
  input id;
  format id z7.;   /* <-- this is the only thing needed to display as z7. */
cards;
3453
123162
124171
1241761
;
run;

OP: Please do not code all in upper case, it really makes code so much harder to look at.

Shmuel
Garnet | Level 18

@RW9, you are right if the target was just displaying the number with leading zeroes.

 

The original query included code:

DATA A(RENAME=(ID1=ID));
SET A;
ID1 = PUT(ID,z7.);
DROP ID;
RUN;

Which I understand that the target is changing type of variable in a dataset.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 7 replies
  • 979 views
  • 0 likes
  • 4 in conversation